Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Saving of a resource and modifying the same after save operation( Saving of a resource and modifying the same after save operation)
icon5.gif   Saving of a resource and modifying the same after save operation [message #1265455] Thu, 06 March 2014 05:44 Go to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi all,
I am doing two operations on a resource (Xtext resource) which I would explain step wise below:

Step 1: I am adding some content to a resource and then saving it.

Step 2: After this I am passing my resource to some other method as input. But this method needs updated saved resource from step 1. But execution happens so quickly that this method is not getting the updated resource.

The code looks like:
resource.getContents().add(model);
    try {
        resource.save(Collections.EMPTY_MAP);
	} catch (IOException e) {
	e.printStackTrace();
	}
			

model.getMyObjs.addAll(readExcel(excelFile, resource).getEntriesList());


readExcel method is not getting the saved resource But if I add Thread.Sleep(milSec) then it works fine. Then code would look like:

resource.getContents().add(model);
    try {
        resource.save(Collections.EMPTY_MAP);
	} catch (IOException e) {
	e.printStackTrace();
	}

    try {
	Thread.sleep(3000);
	} catch (InterruptedException e1) {
	e1.printStackTrace();
        }
			

model.getMyObjs.addAll(readExcel(excelFile, resource).getEntriesList());


Its just a work around. How can I avoid this problem? How to wait until a resource is saved/loaded completely?
Please suggest some ideas.

Thanks in advance Smile





Arshad

[Updated on: Thu, 06 March 2014 05:56]

Report message to a moderator

Re: Saving of a resource and modifying the same after save operation [message #1265622 is a reply to message #1265455] Thu, 06 March 2014 10:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Arshad,

Comments below.

On 06/03/2014 6:44 AM, Arshad Adavani wrote:
> Hi all,
> I am doing two operations on a resource which I would explain step
> wise below:
>
> Step 1: I am adding some content to a resource and then saving it.
What kind of URI are you using? A file: URI to save directly to the
file system, or a platform:/resource URI to save via the Eclipse
workspace APIs?
>
> Step 2: After this I am passing my resource to some other method as
> input. But this method needs updated saved resource from step 1.
That's kind of confusing. What does it do with the resource you pass.
If it needs something that's saved, I'd expect you'd need to pass in the
URI from which it loads, not a resource that already contains exactly
what you're going to operate upon.
> But execution happens so quickly that this method is not getting the
> updated resource.
That doesn't make much sense either. Once the save is completed, the
stream is closed, and at that point, everything should be on the disk.
There's no background thread doing the work of saving...
>
> The code looks like:
>
> resource.getContents().add(model);
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
>
>
> model.getMyObjs.addAll(readExcel(excelFile, resource).getEntriesList());
>
> readExcel method is not getting the saved resource
But you show the resource itself being passed in directly.
> But if I add Thread.Sleep(milSec) then it works fine. Then code would
> look like:
>
>
> resource.getContents().add(model);
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
>
> try {
> Thread.sleep(3000);
> } catch (InterruptedException e1) {
> e1.printStackTrace();
> }
>
>
> model.getMyObjs.addAll(readExcel(excelFile, resource).getEntriesList());
That doesn't really make sense to me. But then it's not clear what URI
you're using to save the resource...
>
> Its just a work around. How can I avoid this problem? How to wait
> until a resource is saved/loaded completely?
That's normally not necessary and certainly not something I'd expect, so
there must be something going on here that you've not explained...
> Please suggest some ideas.
>
> Thanks in advance :)
>
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
icon5.gif  Re: Saving of a resource and modifying the same after save operation [message #1265716 is a reply to message #1265622] Thu, 06 March 2014 13:11 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi Ed,
Thanks for your reply.

I am using platform:/resource URI to save the resource.
The content is getting added to the resource object but still its not visible in the editor.
So before that only I am passing this resource object to some other method.
When I debug I can see the contents in the resource object. But when I run without a delay, I am not getting the updated resource.



Arshad
Re: Saving of a resource and modifying the same after save operation [message #1265861 is a reply to message #1265716] Thu, 06 March 2014 16:54 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Arshad,

Comments below.

On 06/03/2014 2:11 PM, Arshad Adavani wrote:
> Hi Ed,
> Thanks for your reply.
>
> I am using platform:/resource URI to save the resource.
So it's saving via the workspace APIs. You should see that
org.eclipse.emf.ecore.resource.impl.PlatformResourceURIHandlerImpl.PlatformResourceOutputStream.close()
ends up being called called at the end of saving the resource, and after
this point in time, the contents should exist in the file system and
should be seen by anything reading from that resource via the workspace
APIs.
> The content is getting added to the resource object but still its not
> visible in the editor.
A resource delta is sent out, and that delta is by a listener
(resourceChangeListener) in the generated editor. After the delta is
processed, an asyncExec will process the delta further in the UI
thread. This can lead to a delay between when the resource is saved,
and when the editor displays those results.
> So before that only I am passing this resource object to some other
> method.
> When I debug I can see the contents in the resource object. But when I
> run without a delay, I am not getting the updated resource.
It's not clear where you're doing what processing, as I explained how
the resource delta is processed, you can examine what impact that has on
your processing logic. I.e., instead of waiting, perhaps you should be
doing an asyncExec it you're expecting some processing to be done in the
editor. (There are still some things going on that you're not
explaining well.)


Ed Merks
Professional Support: https://www.macromodeling.com/
icon5.gif  Re: Saving of a resource and modifying the same after save operation [message #1266481 is a reply to message #1265861] Fri, 07 March 2014 14:10 Go to previous messageGo to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi Ed,
I have registered a resourcechangeListener like this:

IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IResourceChangeListener listener = new IResourceChangeListener() {
			@Override
			public void resourceChanged(final IResourceChangeEvent event) {


				IResourceDelta delta = event.getDelta();
			}
		};
		workspace.addResourceChangeListener(listener);


Here which property I have to check exactly in delta or how do I have to proceed to know whether my resource has been saved/updated completely?
Could you please give me some implementation idea?


Thanks!


Arshad
Re: Saving of a resource and modifying the same after save operation [message #1266964 is a reply to message #1266481] Sat, 08 March 2014 07:27 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Arshad,

The generated editor has a full implementation.

On 07/03/2014 3:10 PM, Arshad Adavani wrote:
> Hi Ed,
> I have registered a resourcechangeListener like this:
>
> IWorkspace workspace = ResourcesPlugin.getWorkspace();
> IResourceChangeListener listener = new
> IResourceChangeListener() {
> @Override
> public void resourceChanged(final IResourceChangeEvent
> event) {
>
>
> IResourceDelta delta = event.getDelta();
> }
> };
> workspace.addResourceChangeListener(listener);
>
> Here which property I have to check exactly in delta or how do I have
> to proceed to know whether my resource has been saved/updated completely?
> Could you please give me some implementation idea?
>
>
> Thanks!


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] Session not in sync after reconnect
Next Topic:[XCORE] Compare models
Goto Forum:
  


Current Time: Tue Apr 16 10:57:17 GMT 2024

Powered by FUDForum. Page generated in 0.55277 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top