12

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?

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
Roman Barzyczak
  • 3,785
  • 1
  • 30
  • 44

2 Answers2

5

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.

ccjensen
  • 4,578
  • 2
  • 23
  • 25
0

This is probably what you are looking for: Watch Connectivity Framework

More Here: https://developer.apple.com/library/prerelease/ios/documentation/WatchConnectivity/Reference/WatchConnectivity_framework/index.html

And here: https://forums.developer.apple.com/thread/3927

Quoting from the forums.developer.apple.com

Watch apps that shared data with their iOS apps using a shared group container must be redesigned to handle data differently. In watchOS 2, each process must manage its own copy of any shared data in the local container directory. For data that is actually shared and updated by both apps, this requires using the Watch Connectivity framework to move that data between them.

AJ Venturella
  • 4,742
  • 4
  • 33
  • 62