WatchKit Master Data Sync

I have an application structured as follows

IOS application Writes data to basic data that has persistent storage stored in a common application group.

The Watch Kit extension can read data from Core Data that was written by an iOS application.

The problem I am facing is that my iOS application is writing data while the watch dial application is open. I do not receive updates because the context of the object is not synchronized with data on disk.

Is there a way that, since the extension of the clock set is just reading data to update the context and make it load again from the data on disk?

+4
source share
3 answers

My working solution used MMWormhole to send a notification ( NSManagedObjectContextDidSaveNotification ) from the iPhone app to my watch app. In the watch application controller, I used the mergeChangesFromContextDidSaveNotification: method for NSManagedObjectContext.

 // in iPhone app notification handler MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"your.group.container.identifier" optionalDirectory:nil]; [wormhole passMessageObject:notification identifier:@"your notification identifier"]; // in WKInterfaceController awakeWithContext: method MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"your.group.container.identifier" optionalDirectory:nil]; [wormhole listenForMessageWithIdentifier:@"your notification identifier" listener:^(id messageObject) { [self.managedObjectContext mergeChangesFromContextDidSaveNotification:messageObject]; }]; 

Then NSFetchedResultsController did all the other work with UI updates.

You must implement the initWithCoder: and encodeWithCoder: methods from the NSCoding protocol for your NSManagedObject subclasses because MMWormhole uses NSKeyedArchiver as its serialization environment.

 - (id)initWithCoder:(NSCoder *)decoder { NSManagedObjectContext *context = ... // use your NSManagedObjectContext NSPersistentStoreCoordinator *coordinator = ...; //use your NSPersistentStoreCoordinator NSURL *url = (NSURL *)[decoder decodeObjectForKey:@"URIRepresentation"]; NSManagedObjectID *managedObjectID = [coordinator managedObjectIDForURIRepresentation:url]; self = [context existingObjectWithID:managedObjectID error:nil]; return self; } - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:[[self objectID] URIRepresentation] forKey:@"URIRepresentation"]; } 
+2
source

I ran into the same problem. I used - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag in NSManagedObjectContext to get the latest data for the managed object.

+1
source

Perform similar problems. Despite the creation of a common controller of the results obtained in the application group, observing the changes in the context of the managed object and updating the context of the managed object were not possible.

Managed object contexts cache a certain level of the object graph for extraction without reading from a real SQLite repository on disk. The only possible way to get direct synchronization between the two is to send messages through the iOS application in Extension, when the MOC modifies and destroys / restores the Core Data stack from disk every time, and not a great solution at all.

I believe that the use case for direct synchronization between iOS and Extension on initial launch is not a necessity. I hope we get a more deliberate solution to this problem in a future version.

+1
source

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


All Articles