iPhone崩溃CoreData保存 [英] iPhone crash on CoreData save

查看:122
本文介绍了iPhone崩溃CoreData保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与此问题不同的情况,因为提供的解决方案

This is a different situation than this question, as the solution provided doesn't work and the stack is different.

使用coredata保存数据时会定期崩溃。

Periodical crash when I save data using coredata.

堆栈跟踪不是100%清楚这发生在哪里,但我确定这是例行程序被调用。它可以是这个方法中的 save:或下面的一个。

The stack trace isn't 100% clear on where this is happening, but I'm certain it's this routine that's being called. It's either the save: in this method or the one following.

代码:

-(void)saveWine {
    if ([self validInfo]) {
        Wine *wine =  (Wine *)wineToEdit;
        if (wine == nil) {
            wine = (Wine *)[NSEntityDescription insertNewObjectForEntityForName:@"Wine" inManagedObjectContext:self.managedObjectContext];
        }
        wine.uuid = [Utils createUUID];
        wine.name = self.wineNameField.text;
        wine.vineyard = self.vineyardField.text;
        wine.vintage = [[self numberFormatter] numberFromString:self.vintageField.text];
        wine.timeStamp = [NSDate date];
        wine.rating = [NSNumber numberWithInt:self.ratingControl.selectedSegmentIndex];
        wine.partnerRating = [NSNumber numberWithInt:self.partnerRatingControl.selectedSegmentIndex];
        wine.varietal = self.currentVarietal;
        wine.tastingNotes = self.currentTastingNotes;
        wine.dateTasted = self.currentDateTasted;
        wine.tastingLocation = [[ReferenceDataAccessor defaultReferenceDataAccessor] addEntityForType:TASTING_LOCATION 
                                                                                             withName:self.currentWhereTasted];

        id type = [[ReferenceDataAccessor defaultReferenceDataAccessor] entityForType:WINE_TYPE 
                                                                          withOrdinal:self.typeControl.selectedSegmentIndex];
        wine.type = type;
        NSError *error;
        NSLog(@"Saving %@",wine);
        if (![self.managedObjectContext save:&error]) {
            [Utils showAlertMessage:@"There was a problem saving your wine; try restarting the app" withTitle:@"Problem saving"];
            NSLog(@"Error while saving new wine %@, %@", error, [error userInfo]);
        }
    }
    else {
        NSLog(@"ERROR - someone is calling saveWine with invalid info!!");
    }
}

代码addEntityForType:withName :

Code for addEntityForType:withName::

-(id)addEntityForType:(NSString *)type withName:(NSString *)name {

    if ([Utils isStringBlank:name]) {
        return nil;
    }
    id existing = [[ReferenceDataAccessor defaultReferenceDataAccessor] entityForType:type withName:name];

    if (existing != nil) {
        NSLog(@"%@ with name %@ already exists",type,name);
        return existing;
    }


    NSManagedObject *newEntity = [NSEntityDescription insertNewObjectForEntityForName:type 
                                                               inManagedObjectContext:self.managedObjectContext];

    [newEntity setValue:name forKey:@"name"];

    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        [Utils showAlertMessage:[NSString stringWithFormat:@"There was a problem saving a %@",type] withTitle:@"Database Probem"];
        [Utils logErrorFully:error forOperation:[NSString stringWithFormat:@"saving new %@",type ]];
    }
    return newEntity;
}

堆栈跟踪:

Thread 0 Crashed:
0   libSystem.B.dylib               0x311de2d4 __kill + 8
1   libSystem.B.dylib               0x311de2c4 kill + 4
2   libSystem.B.dylib               0x311de2b6 raise + 10
3   libSystem.B.dylib               0x311f2d72 abort + 50
4   libstdc++.6.dylib               0x301dea20 __gnu_cxx::__verbose_terminate_handler() + 376
5   libobjc.A.dylib                 0x319a2594 _objc_terminate + 104
6   libstdc++.6.dylib               0x301dcdf2 __cxxabiv1::__terminate(void (*)()) + 46
7   libstdc++.6.dylib               0x301dce46 std::terminate() + 10
8   libstdc++.6.dylib               0x301dcf16 __cxa_throw + 78
9   libobjc.A.dylib                 0x319a14c4 objc_exception_throw + 64
10  CoreData                        0x3526e83e -[NSManagedObjectContext save:] + 1098
11  Wine Brain                      0x0000651e 0x1000 + 21790
12  Wine Brain                      0x0000525c 0x1000 + 16988
13  Wine Brain                      0x00004894 0x1000 + 14484
14  Wine Brain                      0x00008716 0x1000 + 30486
15  CoreFoundation                  0x31477fe6 -[NSObject(NSObject) performSelector:withObject:withObject:] + 18
16  UIKit                           0x338c14a6 -[UIApplication sendAction:to:from:forEvent:] + 78
17  UIKit                           0x3395c7ae -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 86
18  CoreFoundation                  0x31477fe6 -[NSObject(NSObject) performSelector:withObject:withObject:] + 18
19  UIKit                           0x338c14a6 -[UIApplication sendAction:to:from:forEvent:] + 78
20  UIKit                           0x338c1446 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 26
21  UIKit                           0x338c1418 -[UIControl sendAction:to:forEvent:] + 32
22  UIKit                           0x338c116a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 350
23  UIKit                           0x338c19c8 -[UIControl touchesEnded:withEvent:] + 336
24  UIKit                           0x338b734e -[UIWindow _sendTouchesForEvent:] + 362
25  UIKit                           0x338b6cc8 -[UIWindow sendEvent:] + 256
26  UIKit                           0x338a1fc0 -[UIApplication sendEvent:] + 292
27  UIKit                           0x338a1900 _UIApplicationHandleEvent + 5084
28  GraphicsServices                0x35d66efc PurpleEventCallback + 660
29  CoreFoundation                  0x314656f8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
30  CoreFoundation                  0x314656bc __CFRunLoopDoSource1 + 160
31  CoreFoundation                  0x31457f76 __CFRunLoopRun + 514
32  CoreFoundation                  0x31457c80 CFRunLoopRunSpecific + 224
33  CoreFoundation                  0x31457b88 CFRunLoopRunInMode + 52
34  GraphicsServices                0x35d664a4 GSEventRunModal + 108
35  GraphicsServices                0x35d66550 GSEventRun + 56
36  UIKit                           0x338d5322 -[UIApplication _run] + 406
37  UIKit                           0x338d2e8c UIApplicationMain + 664
38  Wine Brain                      0x000021ba 0x1000 + 4538
39  Wine Brain                      0x00002184 0x1000 + 4484

我不知道为什么我的应用程序的内存位置没有被symbolocated,两个 manavedObjectContext save:调用。这个发生的时间, addEntityForType 一直被调用,为whereTasted实体创建一个新对象,最后保存:对整个葡萄酒对象。

I have no idea why my app's memory locations aren't being symbolocated, but the code paths lead to only two manavedObjectContext save: calls. The time this happend, addEntityForType was called all the way through, creating a new object for the "whereTasted" entity, before the final save: on the entire wine object.

当我再次通过相同的过程,精细。这导致我相信这是与应用程序已经运行一段时间,当添加一个新的位置,但我不知道。

When I go through the same procedure again, it works fine. This leads me to believe it's something to do with the app having been run for a while when adding a new location, but I'm not sure.

有关的任何想法如何

推荐答案

你应该尝试激活Run> Stop on Objective -C异常。它允许我找到一个访问对象变成故障的地方,这是问题的根本原因。 (这是建议,最近为我工作非常好,感谢堪察加)。

You should try to activate "Run > Stop on Objective-C exception". It allowed me to find the place where there was an access to an object turned into fault which was the root cause of the problem. (This was advice that recently worked very well for me. Thanks to Kamchatka.)

这篇关于iPhone崩溃CoreData保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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