简体   繁体   English

在iOS中更新NSManagedObject中的数据时应用崩溃

[英]App crashing while update data in NSManagedObject in ios

Hi in one of my application. 嗨,在我的一个应用程序中。 I am using queues to download a file from server and after file download completes I am updating the status in coredata (Sync type).While Update status in core data db app is crashing continously. 我正在使用队列从服务器下载文件,文件下载完成后,我正在更新coredata(同步类型)中的状态。而核心数据db应用程序中的Update状态持续崩溃。

Here is the code which I used in my app 这是我在应用程序中使用的代码

 **In Download file method**

    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadfileinThread:) object:params] ;

    [operation addObserver:self forKeyPath:@"isFinished" options:0 context:nil];

    [downloadQueue addOperation:operation] ;


    **In Download file in Thread. (Here actual file will download)**

    -(void) downloadfileinThread: 

    {

    [self UpdateDatabase:file with:updatesArray1] ; //updatesArray1 contains dictionaries (Syncstatus:0 like this)

    }

    **DB updation**

    -(void) UpdateDatabase:(id)_object with:(NSMutableArray *)updatesArray
    {

    NSManagedObjectContext *threadManagedObjectContext = [self myManagedContext] ;

[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextObjectsDidChangeNotification object:threadManagedObjectContext] ;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeContextChangesForNotification:) name:NSManagedObjectContextObjectsDidChangeNotification object:threadManagedObjectContext];

  NSManagedObject *object = [threadManagedObjectContext objectWithID:[_object objectID]] ;

            if (updatesArray)

            {
            for (NSDictionary *updatedDic in updatesArray)

            {

         [object setValue:[[updatedDic allValues]lastObject] forKey:[[keyValue allKeys]lastObject]];

             }

             NSError *error;
             bool result = [threadManagedObjectContext save:&error];
                 if (!result)
                 {
                     NSLog(@" error saving context, %@, %@", error, error.userInfo);
                 }
            }
    }        

Crash Message: Terminating app due to uncaught exception 'NSGenericException', reason: ' * Collection <__NSDictionaryM: 0xd01b9e0> was mutated while being enumerated.' 崩溃消息:由于未捕获的异常'NSGenericException'而终止应用程序,原因:' *收集<__ NSDictionaryM:0xd01b9e0>在枚举时发生了变异。

Please help me in resolving this issue. 请帮助我解决此问题。

I am not sure what issue are you facing here, as I can't find anything obvious in your code, but generally this problem occurs, when a mutable object(array or dictionary), get updated(add/remove object) in side a loop where it's been read. 我不确定您在这里面对什么问题,因为我在您的代码中找不到任何明显的问题,但是通常会发生此问题,当可变对象(数组或字典)在a面获取更新(添加/删除对象)时循环读取它的位置。 Try avoiding any such activity in your code. 尝试避免在代码中进行任何此类活动。

Another suggestion will be. 另一个建议是。 Add Exception break point in your code. 在代码中添加异常断点 And run your code with break point on. 并在断点打开的情况下运行您的代码。 This will indicate you to exact line you are facing this exception. 这将指示您正要遇到此异常的确切行。 And attach code with that line, for further clearing. 并在该行中附加代码,以进行进一步清除。

The exception you are getting: 您得到的异常:

* Collection <__NSDictionaryM: 0xd01b9e0> was mutated while being enumerated.

Indicates an NSMutableDictionary instance was changed while it was being enumerated. 指示NSMutableDictionary实例在枚举时已更改。 the M in __NSDictionaryM indicates it's a mutable dictionary. __NSDictionaryM中M表示它是可变字典。 Out of the code you included in your answer, a dictionary is being enumerated in one place that I can see: 在答案中包含的代码中,我可以在一个地方看到一本字典:

[object setValue:[[updatedDic allValues]lastObject] forKey:[[keyValue allKeys]lastObject]];

When [updatedDic allValues] is called Foundation uses Fast Enumeration to compose the array of values. 调用[updatedDic allValues] ,Foundation使用快速枚举来组成值的数组。 While Foundation is doing this, the dictionary object is being changed (mutated) elsewhere in your application. 在Foundation执行此操作时,将在应用程序的其他位置更改(更改)字典对象。 It's likely that whatever part of your program calls this method built an NSMutableArray of NSMutableDictionary s, called this method, and then continued to modify those NSMutableDictionary s. 程序的任何部分调用此方法都可能会构建一个NSMutableArrayNSMutableDictionary ,称为此方法,然后继续修改这些NSMutableDictionary This is what is causing your problem. 这是导致您的问题的原因。 Do not change collections while they are being enumerated. 枚举时不要更改集合。 A very simple fix would be to add immutable copies of the NSMutableDictionary s to the mutable array: 一个非常简单的解决方法是将NSMutableDictionary不可变副本添加到可变数组:

[someArray addObject:[someDictionary copy]];

It's entirely possible though that the exception is actually happening elsewhere, without the stack trace it's impossible to tell. 尽管异常实际上是在其他地方发生的,但完全有可能,如果没有堆栈跟踪就无法分辨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM