Realm , Object is already managed by another Realm - ios

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:.

Related

How to resolve with a exception "Cannot start to manage an object with a realm when it's already managed by another realm"

I'm using Realm Mobile Database in my Xamarin project. And I had a problem when used to realm.Add(obj, update) statement. It throw a exception "Cannot start to manage an object with a realm when it's already managed by another realm". What I didn't see on Swift version on same a demo. I knew when realm object's IsManage is true, and I added a object exist in realm to a another object for update then throw that exception, so how I can update a realm object with a member what exist before that.
var objUpdate = new AccountAccessDB()
{
Id = this.Id, //Id is PrimaryKey
User = this.User // this object existed
};
objUpdate.something.Add(new Object()) // this is that I want to update.
realm.Write(() => {
realm.Add(objUpdate, true);
});
Thanks!
It looks like you're opening to different realms and then try to add an object from one realm to the other realm.
Remember that realm is unique by it's configuration, so if you pass a configuration when opening a realm, you have to use an identical configuration when trying to open the same realm.
// First time you open realm
var realm = Realm.GetInstance("my.realm");
...
// Somewhere else in your code
var realm = Realm.GetInstance(); // <== This is not the same realm!
var myRealm = Realm.GetInstance("my.realm"); // <== This is the same realm
I recently came across the same issue and managed to find the issue.
In my case, I was importing a large amount of unmanaged objects to Realm.
Some of these objects had a property pointing to a custom User object, which I was naively assigning in the object's constructor.
Naturally this means that I was trying to import unmanaged objects that had a property which was pointing to a managed object (created in a different instance, too).

Run Migration on synced Realm

I've created a new object model and when I open my app I get the following error:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid class subset list:
- 'Mod.generalSettings' links to class 'Setting', which is missing from the list of classes managed by the Realm
- 'Mod.contextSettings' links to class 'Setting', which is missing from the list of classes managed by the Realm
- 'Mod.accountSettings' links to class 'Setting', which is missing from the list of classes managed by the Realm'
I don't get this error when I shut down the Realm Object Server that holds my synced Realm. This is the config that runs on launch:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
syncConfiguration: (user, syncServerURL!),
objectTypes: [Dot.self, Mod.self, Setting.self])
This leads me to believe I need to run a migration for my remote realm. How can I do this?
You don't have direct access to Realm files on the server from the client. All you need to do is run a migration locally and the changes will be pushed up to the server.
If you're not deleting any columns, you can run a migration as simply as:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
syncConfiguration: (user, syncServerURL!),
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in },
objectTypes: [Dot.self, Mod.self, Setting.self])
I hope that helps!

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.

Realm realmWithPath method in main bundle crashes on 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:].

Saving custom class into Coredata

Hi i have used attribute type transformable and i have followed the procedure of archiving in one of the Forum .
But it says
CoreData: warning: no NSValueTransformer with class name 'SurveyResults' was found for attribute 'survey' on entity 'SurveyData'
2013-04-30 09:44:16.022 TestReachability[11641:c07] -[SurveyApplication encodeWithCoder:]: unrecognized selector sent to instance 0x845cb00
2013-04-30 09:44:16.023 TestReachability[11641:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SurveyApplication encodeWithCoder:]: unrecognized selector sent to instance 0x845cb00'
Here is my code
SurveyApplication *survey =[[SurveyApplication alloc]init];
survey.name=#"dheeraj";
survey.phone=#"573-356-2598";
NSManagedObject *aStory = [NSEntityDescription insertNewObjectForEntityForName:#"SurveyData"inManagedObjectContext:self.managedObjectContext];
[aStory setValue:survey forKey:#"survey"];
NSError *saveError = [[NSError alloc]init];
if (![self.managedObjectContext save:&saveError]) {
NSLog(#"%#",saveError);
}
SurveyAppplication object is my custom class and im trying to create an object and then store it core data .Could you please help me out.
Thanks
Dheeraj
It's not enough to make the attribute transformable, you also need to arrange for the transformation to happen. You can't just tell Core Data to transform any old object and expect it to know what to do. You have a couple of options:
Don't tell Core Data how to transform the data. In this case, Core Data will attempt to call encodeWithCoder: on your object to convert it to NSData. That's why you get the error that mentions this method-- it's trying to call the method on your class, but that method doesn't exist. In this case your class must conform to NSCoding for the transformation to occur.
Tell Core Data how to transform the data. In this case you create a subclass of NSValueTransformer that performs the transformation. You configure this on the attribute, either in the Core Data model editor or in code. In this case, you must have a custom transformer class that knows how to perform the transformation.

Resources