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)!
Related
I am trying to build an app where I am able to access(read/write) windows/mac shared folders in my local network with swift.
Is there any possible way to do that with swift?
There is an App in the App Store called "FileExplorer" https://apps.apple.com/de/app/fe-file-explorer-file-manager/id510282524 where you can access these shared folders, but I do not know how they programmed this and with which language.
I also tried to access my shared folders via this App and yes it worked I can see my shared folders on my Phone.
But there needs to be a way to do it with swift...
I already tried different things(code bellow).
In the code bellow I tried to access the shared folder of my second mac and write the Text "Write this text to the fileURL as text in iOS using Swift" into the file named "Test.txt" and after that I want to read the same file again.
#IBAction func Button(_ sender: UIButton)
{
var uc = URLComponents()
uc.scheme = "smb"
uc.user = "user"
uc.password = "password"
uc.host = "ip-adress"
uc.path = "document-directory"
// Save data to file
let fileName = "Test"
let url = uc.url
//let DocumentDirURL = URL(fileURLWithPath: "/Users/f/d/t/App/Assets/Apps/TestApp")
let DocumentDirURL = try! URL(resolvingAliasFileAt: url!)
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
print("FilePath: \(fileURL.path)")
let writeString = "Write this text to the fileURL as text in iOS using Swift"
do {
// Write to the file
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
var fullString: String = "" // Used to store the file contents
do {
// Read the file contents
fullString = try String(contentsOf: fileURL, encoding: .utf8)
} catch let error as NSError {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
print("File Text: \(readString)")
}
If I run the code as shown, he always gives me the error
"smb scheme is not supported" and then some additional errors that he can not write/read the file because he can not access it.
When I change the code and only search on the device I am programming on and run the simulator to search for this file everything works fine. So I have problems with "smb".
Thank you for every helpful answer.
you can use amsmb2 library to do this
you can extend the template class provided to connect to download files, write files, list directories -> on an smb share
everything is asynchronous from memory, with the librarys calls including hooks for progress updates on the ui main thread etc
i believe the amsmb2 library function your after might be uploadItem
iOS 13 includes SMB (server message block protocol) support
https://9to5mac.com/2019/06/17/ios-13-beta-2-enables-smb-server-connectivity-in-the-files-app/
I have set up a Realm Object Server on AWS (RHAT) and it appears to be working fine. I can look at the dashboard which tells me how many connections, and how many realms open on port 9080. My code is working fine on the device, updates are happening on the devices realm. But I dont see anything changing in the ROS realm. I may be getting something very basic wrong, but I can't tell what.
func setupRealm() {
// Authenticating the User
let username = "exampleuser"
let password = "examplepassword"
SyncUser.logInWithCredentials(SyncCredentials.usernamePassword(username, password: password), authServerURL: NSURL(string: "http://example.com:9080")!, onCompletion:
{ user, error in
if let user = user {
// Opening a remote Realm
let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
let realm = try! Realm(configuration: config)
// Any changes made to this Realm will be synced across all devices!
} else if let error = error {
// handle error
}
})
}
I call setupRealm() from ViewDidLoad.
My developers need to be able to see the changes to the ROS realm to ensure all is working correctly, but all that is there is the single users realm, structure of tables is accurate, zero data.
I checked the log files on the server and nothing stood out.
Matt
You need to make sure you're still referring to that particular Realm (and its sync configuration) when doing your writes. If you tried to do a write to let realm = try! Realm() even after this login, you'd still be referencing your default Realm, which isn't synchronized.
If you plan to make your entire app synchronized, you can set that particular Configuration as your default Realm's one:
if let user = user {
// Opening a remote Realm
let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
Realm.Configuration.defaultConfiguration = configuration
// All calls to `Realm()` will now use this configuration
}
Otherwise you can always just generate the configuration when you need it (Or have a global constants class that can manage it for you).
let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
let configuration = Realm.Configuration()
configuration.syncConfiguration = SyncConfiguration(user: SyncUser.current, realmURL: realmURL))
let realm = try! Realm(configuration: configuration)
I am using NSUserDefaults.standardUserDefaults() to save my JSON String got from WebService to iPhone memory. When I load it to use with my parser function, the processing speed is so slow. I don't want to use RealmIO or any database because that thing doesn't necessary. I would like to ask is there any way faster than NSUserDefaults? Please check my JSON file (I need store more than 20 files like that)
Instead of saving your data to NSUserDefaults, you should save it to a different file, this will be much more efficient.
Here is how you can do it :
// Build file url
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!
let fileURL = documentsURL.URLByAppendingPathComponent("file_1.json", isDirectory: false)
// Write
let jsonString = "..."
let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
do {
try jsonData?.writeToURL(fileURL, options: NSDataWritingOptions())
} catch {
NSLog("Writing file to `\(fileURL)` failed with error : \(error)")
}
// Read
do {
let jsonData = try NSData(contentsOfURL: fileURL, options: NSDataReadingOptions())
let jsonString = String(data: jsonData, encoding: NSUTF8StringEncoding)
} catch {
NSLog("Reading file at url `\(fileURL)` failed with error : \(error)")
}
NSUserDefaults is not a database. If your JSON documents are more than 100 KB altogether store them into separate documents. Note that every time you change any user default, all the user defaults have to be written to a file. If you have 20 JSON documents of 1MB each, that's writing 20MB or more for every user default the you change.
Really excited about the recent addition of sharing Realm data between apps and extensions. The documentation details how to set the default realm to the app group directory, I've got that working.
Here's what I'm stuck on -- what's the best way to transfer the old database to the new location in the app group?
Based on #segiddins comment, I decided to go with moving the old database to the app group using NSFileManager:
let fileManager = NSFileManager.defaultManager()
//Cache original realm path (documents directory)
let originalDefaultRealmPath = RLMRealm.defaultRealmPath()
//Generate new realm path based on app group
let appGroupURL: NSURL = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.AppGroup")!
let realmPath = appGroupURL.path!.stringByAppendingPathComponent("default.realm")
//Moves the realm to the new location if it hasn't been done previously
if (fileManager.fileExistsAtPath(originalDefaultRealmPath) && !fileManager.fileExistsAtPath(realmPath)) {
var error: NSError?
fileManager.moveItemAtPath(originalDefaultRealmPath, toPath: realmPath, error: &error)
if (error != nil) {
println(error)
}
}
//Set the realm path to the new directory
RLMRealm.setDefaultRealmPath(realmPath)
Hope this will help other reader.
As discussed in https://github.com/realm/realm-cocoa/issues/4490, you can set app group path with below code and use File Manager to move existing db as mention above.
var config = Realm.Configuration()
config.fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)!.appendingPathComponent(dbFilename)
Realm.Configuration.defaultConfiguration = config
Basically I want to retrieve files (e.g. pdf) from other apps (e.g. dropbox) to store and modify later.
I wrote a share-extension for that task and could iterate over NSExtensionItem's and can catch out my files - but I have no idea about their original filename.
I noticed that other apps got the filename - but they're called with the "open with" function in iOS.
So how I get the filename in my share-extension?
Thanks in advance.
You have the filename as lastPathComponent in the URL received:
itemProvider!.first!.loadItemForTypeIdentifier(kUTTypeImage as String, options: nil, completionHandler: {
(provider, error) in
let url = provider as! NSURL
println(url)
let data = NSData(contentsOfURL: url)
let name = url.lastPathComponent!
...
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
})