realm 'Can only delete an object from the Realm it belongs to.'问题

realm报错出现这个问题,如果不是Realm对象使用不一致的话,一般是realm对象跨线程使用了,这时候只需要使用-[RLMRealm resolveThreadSafeReference:]就可以了(realm官方文档例子):

Person *person = [Person new];
person.name = @"Jane";
[realm transactionWithBlock:^{
    [realm addObject:person];
}];
RLMThreadSafeReference *personRef = [RLMThreadSafeReference
    referenceWithThreadConfined:person];
dispatch_async(queue, ^{
    @autoreleasepool {
        RLMRealm *realm = [RLMRealm realmWithConfiguration:realm.configuration
                                                     error:nil];
        Person *person = [realm resolveThreadSafeReference:personRef];
        if (!person) {
            return; // person was deleted
        }
        [realm transactionWithBlock:^{
            person.name = @"Jane Doe";
        }];
    }
});

你可能感兴趣的:(realm 'Can only delete an object from the Realm it belongs to.'问题)