Realm realmWithPath method in main bundle crashes on iOS - ios

I do not use *.realm file in Document directory of app. I use it in main bundle directory.
When I call it:
NSString *path = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"realm"];
RLMRealm *realm = [RLMRealm realmWithPath:path];
It crashes and logs:
Terminating app due to uncaught exception 'RLMException', reason: 'open() failed: Operation not permitted'

The Realm file must be located in a directory to which you have write access in case you want to be able to update any data.
If you only intend to distribute a preset database with your app and access it read only, you must open it read only with [RLMRealm realmWithPath:readOnly:error:].

Related

Realm , Object is already managed by another Realm

Use realm to save data.
Config the realm use defaultconfig.
then , I addOrUpdate some RLMModels
It's success.
I changed the config use
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:#"myname"] URLByAppendingPathExtension:#"realm"];
[RLMRealmConfiguration setDefaultConfiguration:config];
I addOrUpdate the same RLMModels
console print error:
2017-11-09 10:50:18.293801+0800 LNLibBase_Example[96588:8779968]
*** Terminating app due to uncaught exception 'RLMException',
reason: 'Object is already managed by another Realm.
Use create instead to copy it into this Realm.'
As the error message states, an object that is already managed by a Realm cannot be added to a different Realm. What you can do instead is create a copy of it in a different Realm using +createInRealm:withValue:.

Uploading a directory recursively iOS AWS S3

I have been trying to figure out how to upload a directory to S3 for quite some time now using the iOS SDK for AWS.
Currently, I have been having to .zip the directories I have been uploading. Using s3cmd on my EC2 server, I can upload directories no problem. I have also read that it is possible using other SDK's.
This is the code I have now (swift):
let folderPath = DocumentFolder.stringByAppendingString("/folderPath/folder")
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "my-bucket"
uploadRequest.serverSideEncryption = AWSS3ServerSideEncryption.AwsKms
uploadRequest.body = NSURL(fileURLWithPath: folderPath)
uploadRequest.key = "/\(folderName)/"
AWSS3TransferManager.defaultS3TransferManager().upload(uploadRequest)
However, this causes me to receive an NSInvalidArgumentException.
This is the exact error:
2016-02-26 15:11:10.672 Q-Gate[1002:417054] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSConcreteData initWithBytes:length:copy:deallocator:]: absurd length: 18446744073709551615, maximum size: 9223372036854775808 bytes'
*** First throw call stack:
(0x181e55900 0x1814c3f80 0x181e55848 0x182743ef4 0x182743e38 0x1001ccdd0 0x1001cd7fc 0x18242287c 0x182420eb4 0x182420d74 0x182420ca4 0x1824a6ee4 0x182420c30 0x101e81bb0 0x101e8b354 0x182417a88 0x181d390ac 0x18241796c 0x18241782c 0x182417658 0x181e0cefc 0x181e0c990 0x181e0a690 0x181d39680 0x1824a9434 0x182817c40 0x181abfb28 0x181abfa8c 0x181abd028)
libc++abi.dylib: terminating with uncaught exception of type NSException
If I change the path to the .zip, it uploads no problem.
Does anyone know the proper way to upload a directory to s3 using their iOS SDK?
You cannot pass a directory as body. You need to recursively list files in the directory and call - upload: for each file.
AWSS3TransferManager() is not a valid initializer for this class. The reason for the exception is
`- init` is not a valid initializer. Use `+ defaultS3TransferManager` or `+ S3TransferManagerForKey:` instead.
You need to use AWSS3TransferManager.defaultS3TransferManager() or AWSS3TransferManager(forKey: "YourKey"). Read API docs for more details.

NSFileManager -createFileAtPath fails with NSInvalidArgumentException

Maybe I've just missed something in the documentation, but I can't find anything that says this behavior should have changed in iOS 8.
My app sets the current working directory to the Documents directory, then tries to create a file there using NSFileManager -createFileAtPath. Prior to iOS 8 this works fine. On devices running iOS 8, I get the following on the call to -createFileAtPath:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSFileManager fileSystemRepresentationWithPath:]: nil or empty path argument'
Here is a minimal code snippet that reproduces the problem:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:docsDir];
[[NSFileManager defaultManager] createFileAtPath:#"temp.dat" contents:nil attributes:nil];
Note the path argument on createFileAtPath is "temp.dat" - if I change it to "./temp.dat", the call succeeds.
Am I doing something stupid and this was just "accidentally" working in prior iOS releases? Or did they intentionally change the behavior here? Or is this a bug in iOS 8? Other NSFileManager methods that take a path argument seem to be okay with just a filename (e.g. -removeItemAtPath:#"temp.dat" error:&err succeeds).
Edited to add:
This only happens on a physical device. In the simulator, the above call to createFileAtPath succeeds.
I opened a bug with Apple and they closed it as a duplicate. While that doesn't necessarily confirm that they consider it a bug, it at least confirms that I'm not the first to encounter this, and that the behavior did indeed change with iOS 8.
For now, the solution is to prepend ./ to the filename, or supply an absolute path.

Error copying file from app bundle

I used the FireFox add-on SQLite Manager, created a database, which saved to my desktop as "DB.sqlite". I copied the file into my supporting files for the project. But when I run the app, immediately I get the error
"Assertion failure in -[AppDelegate copyDatabaseIfNeeded], /Users/Mac/Desktop/Note/Note/AppDelegate.m:32 2014-08-19 23:38:02.830
Note[28309:60b] Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Failed to create writable
database file with message 'The operation couldn’t be completed.
(Cocoa error 4.)'.' First throw call stack: "...
Here is the App Delegate Code where the error takes place
-(void) copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!success)
{
NSString *defaultDBPath = [[ [NSBundle mainBundle
] resourcePath] stringByAppendingPathComponent:#"DB.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
I am very new to Sqlite, so I maybe I didn't create a database correctly in the FireFox Sqlite manager, or maybe I didn't "properly" copy the .sqlite file in? (I did check the target membership in the sqlite and it correctly has my project selected. Also, the .sqlite file names all match up perfectly.)
In iOS, you can't write a file in your app's bundle. The entire bundle is read-only.
You should create the database (sqlite file) inside the NSDocumentDirectory in order to work on it.
Please visit site below for tutorial:
http://objectivecwithsuraj.blogspot.in/2012/10/database-implementation-using-sqlite-in.html
Sample Code:
https://github.com/surajwebo/SQLite-Demonstration-

Download localizable 'strings' files from server

I would like to be able to add/edit languages in my iOS applications. Therefore, I followed the approach in this question, could not get it to work however.
This is what I did:
I created a folder myFolder and dragged the localization folders (en.lproj, ...) from my XCode project into that folder. Then I renamed it to myFolder.bundle.
Then I tried to load that whole bundle from a URL like so:
NSURL *url = [NSURL URLWithString:#"http://192.168.0.12/language_plists/Localizable.bundle"];
NSBundle *bundle = [[NSBundle alloc] initWithURL:url];
which throws the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSBundle initWithURL:]: non-file URL argument'
Explicitly it will not work using a non-file URL, because NSBundle only deals with File URLs. At best if you were to download, you would have to download into either Cache or Document locations, and you would have to do so explicitly.
Further, You will need to re-submit the App in order to get many of the benefits of localization, because your App Store entry will only contain languages that can be tested before download.
You have option with this github
Download your json file in your document directory and access its path.
i.e.
//Download the app from server and save it to Document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths lastObject] stringByAppendingPathComponent:#"strings.json"];

Resources