Last active
March 19, 2017 14:20
-
-
Save hsylife/6d4c68861b02f1d84e484cc2f4e1f2db to your computer and use it in GitHub Desktop.
RealmファイルをwatchOSにfileTransferしたときの実機で生じるエラーと解決方法 ref: http://qiita.com/hsylife/items/7dc70b52e34dd941c9e4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When the file was received | |
func session(_ session: WCSession, didReceive file: WCSessionFile) { | |
//set the recieved file to default Realm file | |
var config = Realm.Configuration() | |
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) | |
let documentsDirectory = paths[0] | |
let realmURL = documentsDirectory.appendingPathComponent("data.realm") | |
if FileManager.default.fileExists(atPath: realmURL.path){ | |
try! FileManager.default.removeItem(at: realmURL) | |
} | |
try! FileManager.default.copyItem(at: file.fileURL, to: realmURL) | |
config.fileURL = realmURL | |
Realm.Configuration.defaultConfiguration = config | |
// display the first of realm objects | |
let realm = try! Realm() | |
if let firstField = realm.objects(Field.self).first{ | |
realmLabel.setText(firstField.text) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When the file was received | |
func session(_ session: WCSession, didReceive file: WCSessionFile) { | |
//set the recieved file to default Realm file | |
var config = Realm.Configuration() | |
config.fileURL = file.fileURL | |
Realm.Configuration.defaultConfiguration = config | |
// display the first of realm objects | |
let realm = try! Realm() | |
if let firstField = realm.objects(Field.self).first{ | |
realmLabel.setText(firstField.text) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment