美文网首页
KVO 崩溃问题

KVO 崩溃问题

作者: PittWong | 来源:发表于2016-05-23 15:31 被阅读596次

    KVO崩溃问题 忘记了注销监听

    - (void)loadData {
        [[KINGNetWorkHandler sharedHandler]getEmergencyInfoWithEmId:self.model.emId Block:^(NSDictionary *keyDict, NSError *error) {
            if (error || [keyDict[@"code"] integerValue]!=0) {return ;}
            [self.model setValuesForKeysWithDictionary:keyDict];
            [[KINGNetWorkHandler sharedHandler] getTaskInfoWithTaskId:self.model.taskId Block:^(NSDictionary *keyDict, NSError *error) {    
                if (error || [keyDict[@"code"] integerValue]!=0) {return ;}
                self.currentTask = [KINGTaskInfoNetModel mj_objectWithKeyValues:keyDict];
                self.currentTask.isFromEmergency = YES;
                //添加监听当前task的status状态
                [self.currentTask addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
            }];
        }];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
        if ([object isKindOfClass:[KINGTaskInfoNetModel class]]) {        
            self.model.emStatus = [(KINGTaskInfoNetModel *)object status];
            if ([self.model.emStatusString isEqualToString:@"已处理"]) {
                self.listModel.currentEM = nil;
                [KINGDataBase saveEmergencyListNetModel:self.listModel];
            }
        }
    }
    

    *//问题在这里 忘记了dealloc删除监听-------------------------

    - (void)dealloc {
        [self.currentTask removeObserver:self forKeyPath:@"status" context:nil];//KVO崩溃,添加监听者必须记得取消监听 否则就崩给你看
    }
    

    *//问题在这里 忘记了dealloc删除监听-------------------------

    相关文章

      网友评论

          本文标题:KVO 崩溃问题

          本文链接:https://www.haomeiwen.com/subject/olvarttx.html