-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental/autosave freeze fix #25
base: v0.3.x
Are you sure you want to change the base?
Conversation
otherwise the file package gets deleted when the document object is closed.
Hi @adib, sorry for the massive delay in getting back to you on this. I'm really not keen on the solution in this pull request I have to admit. Completing bypassing the standard save method is bound to have bad consequences; if not in your app, at least in mine and others. What I don't understand is why I'm so far unable to reproduce the hang in our own app. Are you able to reproduce it in a standalone app that I can play with, perhaps? |
Frankly reproducing it consistently is a real challenge. The app has a number of background "refresh" jobs and upping the frequency of these refresh jobs significantly increases the probability of the app freezing. The biggie is "probability", means it doesn't always freeze. But it does happen intermittently. Probably your app (Sandvox?) doesn't have asynchronous background refreshes that writes to Core Data, which makes it difficult for you to reproduce. So at 30 seconds refresh interval and two different data files (which amounts to four refreshes per 30 seconds since each data file handles two "accounts"), the app usually freezes before 10 minutes of run time on my 2011 MacBook Air (which is dual core with hyper-threading). Whenever it freezes, it's always deadlocks on the stack trace that I've pasted in #24. But the freezing stops after I added this patch. Some of my users also confirmed that the freezes went away, so I'm pretty confident of the fix. I could issue a license of the app to you and send you an old copy to play it yourself, if you like. But at this point of time I can't create a standalone app that can predictably reproduce the issue. In any case Core Data should handle coordination issues by itself, as indicated by WWDC 2014 Extensions Part 2 (session 217). |
Can you expand a bit more upon what these "background refresh jobs" are actually doing? My reason for hooking into the file coordinator stuff is not to protect the Core Data file. Instead it's to co-ordinate the handling of content for saving into the document package. Because that all gets stashed into an ivar, it needs to be protected so that there's only ever one use of it happening at once. I tried to hook into |
These "background refresh jobs" are polling the server for new data (JSON REST), converting it to Core Data objects and then saving the document. If what you need is to coordinate the document package (as in intra-process coordination), you may want to leverage a GCD serial queue instead of file coordination. At least for the in-place AutoSave where file coordination is definitely overkill. |
So you're modifying the MOC, and programatically triggering an autosave? Just trying to make sure you're not saving the MOC directly. Yeah, I think moving away from file coordination is likely a good idea, and GCD the most sensible choice. I do have an approach built on that that could work. Maybe I haven't thought it through enough yet though:
I think the semaphore can be achieved by a quick call to |
Well it creates a child context of the main context and save the child. Autosave kicks in automatically via the autosave interval. It's not the most performant way to do this, but I didn't want to bother with sibling context coordination when I wrote that. Do you mean the If you're hesitant to maintain another queue, borrowing the root MOC's is probably a good alternative. |
On second thought, a short |
What would |
'this' is the BSManagedDocument instance. @synchronized(this) { Thank you Sasmito Adibowo
|
I had this freeze very frequently in my App - and I did not do any heavy background work. All I've done is to manually trigger autosave every 30 sec. For me - "system imposed" autosaves did work, and manual autosaves - made the app hang. Did not test with it since then - I simply gave up driving the autosave myself. Somehowe the OS does it for me (via another mechanism, of the draft-allways-saved on the side, but you need to hit "Save" to commit the changes to the "real" document). |
This fixes the freezing issue found in #24. Apparently
NSDocument
freezes during in-place autosave on file coordination, thus the solution is to have our own in-place autosave implementation that skips it. Since the persistent store isn't expected to move while being open anyway, it's likely safe to skip callingNSFileCoordinator
during autosave.I've incorporated this in my application and sent out a beta release and seems stable. Furthermore I'm making that beta live now (cross fingers ;-) )