Basic data with WatchKit and iOS App

This question has already been asked, but received no answers .

My iOS application and my WatchKit extension share an application group that stores the master data store. The Core Data (simplified) model is a Person object that has an Age attribute. Both the application and the extension can change the age as desired.

If the value for a person changes to a clock, the application does not reflect it, unless I close and start the application. The same thing happens in the opposite direction.

If I try to change the age on the watch to 20 and change the age on the phone to 30, I get an error message related to merge conflicts because I do not use the most modern version of NSManagedObject on one of the devices (depending on which one changes age at last).

How can I combine two teams without any problems?

+6
source share
2 answers

Since alerts will not be used between different processes, you need to use Darwin notifications. You can easily use the openParentApplication:reply: method to send updates from the Watch Extension application to the iOS application, but Apple has no way to send events from the iOS application to the viewing extension.

I suggest you use the MMWormhole (or you own the implementation of Darwin notifications) to send Darwin notifications when the Core Data Object is updated. Here is an article describing the operation of the library.

If you update the Core Data object in a time zone extension, send a Darwin delivery notification (or send a message) to the iOS application, telling the application that it needs to update the Core Data object. The same approach can be used in a different direction: from an iOS application to an extension of the time zone.

Also, if the objects are really simple, then CoreData might be redundant. You can use MMWormhole to simply cache the lightweight dictionary that is used at both ends. I have been using this approach for several weeks and it works flawlessly in both directions.

+3
source

This is because you are using a different managed entity context that cannot communicate through notifications. Therefore, publishing a notification like NSManagedObjectContextObjectsDidChangeNotification will not work, because NSNotificationCenter will not send a notification from your iOS application to the extension, since they are both different processes running in different memory spaces. I came across the same thing in my application. Before use, you must update the object. Basically, you make a manual mistake. For performance, you probably want to know when to do it and when not to do it. You can update an object by passing it [NSManagedObjectContext refreshObject: mergeChanges:] .

+1
source

Source: https://habr.com/ru/post/983201/


All Articles