Data content changes when Coredata didsavenotification is received

时间:2016-04-07 10:41:33

标签: ios swift core-data nsnotificationcenter

I'm having a terrible problem. I'm saving a managedObject "Customer" with field sync as false using my main MOC. Then after syncing with server, I use a private MOC to save the Customer with sync field as true. When I save this, notification is passed and the main MOC saves the customer correctly. Then when I edit that customer, the same process happens. But this time, even if the sync field is true, upto the point where private MOC saves it, the notification got at the other end contains Customer with sync as false. If I repeat the process, sync becomes true. Instead of repeating, if I am saving some other data using the private MOC, then with that notification, Customer also comes with sync field as true. I don't understand why notification method is not working correctly. In addObserver of didsavenotification, I have specified the object as the private MOC itself. Somebody please help. These are the code snippets prominent in this flow.

//Assigning false

selectedCustomer.synced = NSNumber(bool: false)

//Saving using main MOC

try managedObjectContext.save()

//Assigning synced details to a dictionary

let cust: CustomerDetails = CustomerDetails(data: customer) cust.synced = true

//Assigning dictionary to customer managedObject fetched from coredata using private MOC

assignData(customerDetails, toCustomer: customer)

//Saving the private MOC

try syncManagedObjectContext.save()

//Getting notification and saving

managedObjectContext.performBlock { () -> Void in self.managedObjectContext.mergeChangesFromContextDidSaveNotification(notification) }

//Registration of notification

let notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.addObserver(self, selector: #selector(AppDelegate.managedObjectContextDidSave(_:)), name: NSManagedObjectContextDidSaveNotification, object: syncManagedObjectContext)

EDIT:

These are the definitions of the MOCs I'm using

lazy var managedObjectContext: NSManagedObjectContext = {
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

lazy var syncManagedObjectContext: NSManagedObjectContext = {
    let coordinator = self.managedObjectContext.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
    managedObjectContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

1 个答案:

答案 0 :(得分:1)

显示您如何定义MOC。

他们是父母的孩子吗?

他们是NSPersistentStoreCoordinator的兄弟姐妹吗?

这会产生影响。

我还会打开-com.apple.CoreData.ConcurrencyDebug 1并再次运行您的应用,因为这听起来像是一个线程问题。

更新1

由于您的NSManagedObjectContext实例是兄弟姐妹,您确实需要打开货币调试标志并再次运行。这听起来像是一个线程问题。

更新2

如果这不是线程问题,那么您的问题中没有很多代码可以尝试推断出答案。

我的下一个建议是将您的私人MOC从兄弟姐妹转移到孩子(Apple的线程文档讨论此问题)并停止使用该通知(儿童MOC不必要)。

如果问题仍然存在,那么您将需要在调试器中跟踪它并找出问题所在的位置。如果没有在错误的线程上触及错误的对象,它仍然可能是一个线程问题"问题。您认为财产是否会被更改?使用调试器是唯一确定的方法。