1
votes

I have a Cocoa application which contains a Today Extension. I use Core Data (SQLite) to store and share data between those two. Both apps share a group container to have access to the same data store. Both applications do not only read but as well write data which leads to problems. At times either of the applications throws an error. I experienced three different error types so far:

Could not merge changes


Error in CoreDragRemoveTrackingHandler: -1856

Error in CoreDragRemoveReceiveHandler: -1856


CoreData: error: NULL _cd_rawData but the object is not being turned into a fault


Error in CoreDragRemoveReceiveHandler: -1856 CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)

From the article Sharing Data with Your Containing App in the iOS dev library (this is not iOS/Cocoa-Touch, but OSX/Cocoa - but the same should apply) I have learned:

To avoid data corruption, you must synchronize data accesses. Use Core Data, SQLite, or Posix locks to help coordinate data access in a shared container.

Can someone please point me into a direction what this exactly means?

1
It would help if you edited your question to include the lines of code where these exceptions occur. Also, the complete error messages (Core Data might say "Could not merge changes", but it will say a whole lot more than just that). - Tom Harrington
The exceptions are not bound to any lines of my code. Some cryptic gibberish might be highlighted in green when those happen. The only thing I could show would be mangedObjectContext.save(&error) which then results into an exception and/or returning false and error containing "Could not merge changes". The error does not contain anything beside that. But my question aims more for a general description, not specific to my code. Something that explains what Apple means by the above quote. - udondan

1 Answers

3
votes

The article Change Management in the Mac Dev Library helped me to understand the problem. Turns out I was missing some basic concepts about Core Data.

A managed object context has a merge policy, which by default is set to NSErrorMergePolicy, means it is resulting in an error: "Could not merge changes"

Other policy options are:

  • NSMergeByPropertyStoreTrumpMergePolicy
  • NSMergeByPropertyObjectTrumpMergePolicy
  • NSOverwriteMergePolicy
  • NSRollbackMergePolicy

In my case setting the NSMergeByPropertyObjectTrumpMergePolicy was the best solution. Example implementation in Swift:

var managedObjectContext = NSManagedObjectContext()
managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

There are other concepts, e.g. notifications via NSManagedObjectContextDidSave. But I'm not sure this would work with two different applications.

I'm not yet sure about the other errors. The merge conflict was the only reproducible case. The other problems appeared only once or twice over a period of two weeks. I don't think those were related to the merging conflict. I'll update the answer if those happen again.