Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use coredata from iPhone on AppleWatch OS2?

I tried send it by fileTransfer method:

let modelURL = NSBundle.mainBundle().URLForResource("my_app", withExtension: "momd")!

WCSession.defaultSession().transferFile(modelURL, metadata:nil)

but I get error:

Optional(Error Domain=WCErrorDomain Code=7008 "Invalid parameter passed to WatchConnectivity API." UserInfo={NSLocalizedDescription=Invalid parameter passed to WatchConnectivity API., NSLocalizedRecoverySuggestion=Only pass parameters of correct type.})

Do you have any idea how to sync CoreData between iPhone and WatchOS2?

like image 474
Roman Barzyczak Avatar asked Sep 20 '15 14:09

Roman Barzyczak


1 Answers

You are trying to send the entire “momd” directory. The transfer file API of WatchConnectivity does not seem to support transferring directories, and is therefore returning an error in -session:didFinishFileTransfer:error:

To resolve this problem you have a couple of options:

  1. Serialize the momd directory into a single file and then deserialize on the receiving side (using something like zip, etc)
  2. Create a transfer format for transferring specific pieces of information from the database.
    • The project would pull a specific piece out of the database, and send it across. The receiving side would then add that piece of content to its own database. You would probably use the transferUserInfo API with this solution.

Solution number 2 is probably the best one as it allows you to send only the changes that have been made instead of the entire database each time a change is made, but will be more work.

like image 171
ccjensen Avatar answered Oct 23 '22 10:10

ccjensen