Realm , Object is already managed by another Realm

Viewed 4070
  1. Use realm to save data.
  2. Config the realm use defaultconfig.
  3. then , I addOrUpdate some RLMModels
  4. It's success.
  5. I changed the config use

    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"myname"] URLByAppendingPathExtension:@"realm"];
    [RLMRealmConfiguration setDefaultConfiguration:config];
    
  6. I addOrUpdate the same RLMModels

  7. 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.'
    
1 Answers

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