The following is a test method for communicating between the watchOS and iOS components of my app:
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler {
NSArray *responseArray = @[@"hello", "world"];
NSDictionary *responseDict = @{@"response": responseArray};
replyHandler(response);
}
This works perfectly - in the reply handler on the watch I can log the contents of responseDict and see the objects @"hello"
and @"world"
. However, if I change responseArray to contain NSManagedObject instances (for sending actual data to the watch), the sendMessage error handler is triggered with an error saying Payload could not be delivered
. Before I change my database structure to include a uuid for the entities I need to send (so I can send them represented by their UUID in NSString format), I just wanted to check: is it actually possible to send NSManagedObject instances to watchOS?
No, it's not possible to send NSManagedObject
instances between contexts, threads, or devices.
A managed object only exists within its managed object context. Its data would be nil, if you tried to access or copy it outside its context.
If your Core Data persistent store is on the phone, but you want to display a managed object's data on the watch, you'd first to move the data from the managed object into another type (e.g., a dictionary), and then send that data to the watch.
See this answer for more details.