简体   繁体   中英

sending a database/Realm using Multipeer Connectivity

I have an admin and a user app. Basically, I will build the database(realm) using Admin app then send the whole database to the user app.

Here is the path to my database:

let directory: NSURL =  NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.example.file")!

let fileURL = directory.URLByAppendingPathComponent("test.realm")

//What should I do to the path so I can send it via MultiPeerConnectivity : MCSession

try session.sendData("my Realm File To Send", toPeers: session.connectedPeers, withMode: .Reliable)

I'd recommend to write a compacted copy of your Realm file via writeCopyToURL(fileURL: _, encryptionKey: _) . Read more about Realm's file size growth behavior to understand why that is a good idea.

You can then simply read the file contents via NSData(contentsOfURL: _) .

let parentURL = fileURL.URLByDeletingLastPathComponent!
let compactedFileURL = parentURL.URLByAppendingPathComponent("compact.realm")
try! realm.writeCopyToURL(compactedFileURL)
let data = NSData(contentsOfURL: compactedFileURL)!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM