Search code examples
swift3realmwatchkit

Realm database for iOS app with Watch Extension


Help to understand the issue related to Realm. There is an application for iOS and Watch Extension to it. I was able to connect to each of them Realm base, but when you add information to one - for example, in the iOS application, it does not appear on the Watch. How can I synchronize the display?

I use this function in both cases:

func setDefaultRealmPath()
{
let directory: URL =  FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.gurman.watchTestApp")!

let fileRealmURL = directory.appendingPathComponent("db.realm")
realm = try! Realm(fileURL: fileRealmURL)

var config = Realm.Configuration.defaultConfiguration
config.fileURL = fileRealmURL

Realm.Configuration.defaultConfiguration = config

print("file url: \(realm.configuration.fileURL!)")
}

Help please!


Solution

  • While in watchOS 1, watch apps were shared extensions of the parent app with access to shared files, as of watchOS 2, this is no longer the case.

    Apps on watchOS 2 are completely separate from their parent iOS apps (To the point where I believe they can function while the parent app is closed). As such, it's necessary for you to communicate changes made in the iOS app to the watch.

    You can use WCSession in the WatchConnectivity framework to send data between the two platforms. If you have a pre-bundled Realm file containing an initial set of data, you should be able to send the entire file over when the app initially launches. After that point, you should only send the key-value data of what changed to the watch so it remains up to date.