错误:对象已被删除或无效. (领域) [英] Error: Object has been deleted or invalidated. (Realm)

查看:296
本文介绍了错误:对象已被删除或无效. (领域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时(很少但会发生),当尝试使用属性或在AFnetworking Block内修改模型对象时,出现错误Object has been deleted or invalidated..谁能帮助我找出我在做错什么?

Sometimes (rarely but occurs) I got error Object has been deleted or invalidated., when trying to modify my model object with a property or inside AFnetworking Block. Can anyone help me to find what I'm doing wrong?

错误-情况1:

代码:

- (void)myFunction {
    Model *model = [Model objectForPrimaryKey:1];

    if (model) {
        [self updateModel:model];
    }
}

- (void)updateModel:(Model *)model {

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager PUT:@"http://www.example.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

        [[RLMRealm defaultRealm] beginWriteTransaction];
       model.updated = YES; // Crash: Object has been deleted or invalidated.
        [[RLMRealm defaultRealm] commitWriteTransaction];

    } failure:nil];
}


错误-情况2:

属性:

@property (strong, nonatomic) Model *model;

代码:

- (void)myFunction {
    Model *model = [Model objectForPrimaryKey:1];

    if (model) {
        self.model = model;

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Would you like to edit the model?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        [alert show];
    }
}

UIAlertView委托:

UIAlertView Delegate:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[RLMRealm defaultRealm] beginWriteTransaction];
        self.model.updated = YES; // Crash: Object has been deleted or invalidated.
        [[RLMRealm defaultRealm] commitWriteTransaction];
    }
}

谢谢.

推荐答案

例如1,网络请求是在不同的操作队列上异步执行的,并回调到主线程,很可能您已在其中放置了一些代码,这可以同时由用户操作触发并同时删除对象.您持有的模型对象参考将自动更新并反映出删除.由于无法修改已删除的对象,因此会出现错误.

As for example 1 the network request is executed asynchronous on a different operation queue and calls back to the main thread, it's very likely that you have some code in place, which can be triggered meanwhile by a user action and delete the object concurrently. The model object reference you're holding will be automatically updated and reflect the deletion. Because a deleted object can't be modified it comes to the error.

示例2还涉及并发.您的代码首先检索模型对象,然后显示警报视图.显示UIAlertView时,主线程未被阻止.从理论上讲,与此同时,可以完成之前排队的网络操作,可以分派完成块,并且会删除模型对象.用户确认修改.您的委托实现已被调用,但希望先前检索到的对象仍然存在.

Also example 2 involves concurrency. Your code retrieves the model object first, then it shows the alert view. While the UIAlertView is shown, the main thread is not blocked. Theoretically at the same time, a network operation enqueued before could finish, the completion block could be dispatched, a deletion of the model object occurs. The user confirms the modifications. Your implementation of the delegate is called, but expects the previously retrieved object to be still existing.

避免崩溃的一种可能性是仅存储一个主键而不是完整的模型对象引用,这样可以不断更新并反映最近的更改.主键将保持不变,并且应始终能够识别您的对象.然后,您可以稍后使用主键在写事务中直接检索对象.

One possibility to avoid the crashes is to store just a primary key instead of a full model object reference, which would keep updating and reflecting recent changes. The primary key will stay constant and should always be able to identify your object. You can then use the primary key later to retrieve the object directly in your write transaction.

请注意,如果同时修改数据,则取决于您自己来定义应用程序的行为.您可以尝试通过在副本中保留更多数据来重新创建对象,或者忽略事件并让删除获胜,或者通过适当地限制UI来确保不会发生冲突的修改.您必须提出解决冲突的策略.

Note that it will be in any case up to you to define how your app behaves, if your data was modified concurrently. You can try to recreate the object, by keeping more data in copy around, or ignore the event and let the delete win, or make sure that there won't happen conflicting modifications by restricting the UI adequately. You have to come up with a conflict resolution strategy.

这篇关于错误:对象已被删除或无效. (领域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆