I added NSMergePolicyType for my context but every time that I have a conflict the app is crash.
I added constraints to the table in xcdatamodeld (with "id" )

This is my context:
lazy var managedObjectContext: NSManagedObjectContext? =
{
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
managedObjectContext.mergePolicy = NSMergePolicyType.overwriteMergePolicyType
return managedObjectContext
}()
And this is the save:
func saveContext(onSuccess: (() -> Void)?) {
if let moc = self.managedObjectContext {
moc.perform {
if moc.hasChanges {
do {
try moc.save()
onSuccess?()
} catch {
print("SaveContext error \(error)")
}
}
}
}
But when i have a conflict with the id the app is crashed
Error:
-[_SwiftValue resolveConflicts:error:]: unrecognized selector sent to instance 0x1d0256680
Core data logs:
CoreData: annotation: sql connection fetch time: 0.0001s
CoreData: annotation: total fetch execution time: 0.0001s for 1 rows.
CoreData: annotation: fault fulfilled from database for :
0xd000000000080004 <x-coredata://E7F35452-965D-460D-AAFE-
5ED17B8E0174/DatapulseEntity/p2>
CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, t0.Z_OPT, t0.ZCREATEDTIME,
t0.ZDELETEDTIME, t0.ZDESCRIPTION_VALUE, t0.ZID, t0.ZIS_MY_FAVORITE,
t0.ZIS_SELF_LIKED, t0.ZIS_SELF_SAVED_OFFLINE, t0.ZIS_SELF_VIEWED,
t0.ZIS_SHARED, t0.ZLIKES, t0.ZNAME, t0.ZPARENT_ID, t0.ZTHUMBNAIL,
t0.ZTYPE, t0.ZUPDATEDTIME, t0.ZVIEWS, t0.ZLINKS, t0.ZREFRESHRATE FROM ZKNOWLEDGEBASEENTITY t0 WHERE t0.Z_PK = ?
CoreData: annotation: sql connection fetch time: 0.0000s
CoreData: annotation: total fetch execution time: 0.0000s for 1 rows.
CoreData: annotation: fault fulfilled from database for :
0xd000000000040004 <x-coredata://E7F35452-965D-460D-AAFE-5ED17B8E0174/KnowledgeBaseEntity/p1>
2018-02-04 16:18:30.149318+0200[49571:18233278] -[_SwiftValue resolveConflicts:error:]: unrecognized selector sent to instance 0x1d0256680
Everting is working find , Only when i have an id conflict it crash. (try on iphone 8 , ios 11.2)
Found the issue:
The mergePolicy is a type of "Any" That means that you can insert any object widout getting an error.
BUT mergePolicy must get a mergePolicy object, any other object will cause a crash in the system.
What I have done is to pass the mergePolicyType and not the object.
Crash!:
managedObjectContext.mergePolicy = NSMergePolicyType.overwriteMergePolicyType
Work:
managedObjectContext.mergePolicy = NSMergePolicy(merge: NSMergePolicyType.overwriteMergePolicyType)
Work also:
managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With