添加到Core Data时应用程序崩溃

时间:2011-04-05 08:39:00

标签: iphone objective-c core-data

有谁能告诉我这次崩溃的原因是什么,因为我不知道会出现什么问题。这里是崩溃日志:

Incident Identifier: 517593EA-7CBC-4790-A924-966938AAA01B
CrashReporter Key:   179f2e12142cea96d45f2f6475c7f89a7a702d90
Hardware Model:      iPhone1,2
Process:         AppName [3579]
Path:            /var/mobile/Applications/9FBBBD37-2A31-4240-A8AC-64E5BD0B00C0/AppName.app/AppName
Identifier:      AppName
Version:         ??? (???)
Code Type:       ARM (Native)
Parent Process:  launchd [1]

Date/Time:       2011-04-04 15:51:32.466 +0200
OS Version:      iPhone OS 4.2.1 (8C148)
Report Version:  104

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00000004
Crashed Thread:  9

Thread 0:
0   libSystem.B.dylib               0x35d5b3b0 mach_msg_trap + 20
1   libSystem.B.dylib               0x35d5d894 mach_msg + 60
2   CoreFoundation                  0x37521f7c __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x37521780 __CFRunLoopRun + 400
4   CoreFoundation                  0x37521504 CFRunLoopRunSpecific + 220
5   CoreFoundation                  0x37521412 CFRunLoopRunInMode + 54
6   GraphicsServices                0x33e76d1c GSEventRunModal + 188
7   UIKit                           0x3591d574 -[UIApplication _run] + 580
8   UIKit                           0x3591a550 UIApplicationMain + 964
9   AppName                         0x00002c9a main (main.m:13)
10  AppName                         0x00002c64 start + 32

Thread 1:
0   libSystem.B.dylib               0x35d8f974 kevent + 24
1   libSystem.B.dylib               0x35e5e2fc _dispatch_mgr_invoke + 88
2   libSystem.B.dylib               0x35e5dd68 _dispatch_queue_invoke + 96
3   libSystem.B.dylib               0x35e5d788 _dispatch_worker_thread2 + 120
4   libSystem.B.dylib               0x35de6970 _pthread_wqthread + 392
5   libSystem.B.dylib               0x35ddd2fc start_wqthread + 0

Thread 2:
0   libSystem.B.dylib               0x35d5b3b0 mach_msg_trap + 20
1   libSystem.B.dylib               0x35d5d894 mach_msg + 60
2   CoreFoundation                  0x37521f7c __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x37521780 __CFRunLoopRun + 400
4   CoreFoundation                  0x37521504 CFRunLoopRunSpecific + 220
5   CoreFoundation                  0x37521412 CFRunLoopRunInMode + 54
6   WebCore                         0x3318bd14 RunWebThread(void*) + 524
7   libSystem.B.dylib               0x35de5b44 _pthread_start + 364
8   libSystem.B.dylib               0x35dd77a4 thread_start + 0

Thread 3:
0   libSystem.B.dylib               0x35de454c __semwait_signal + 24
1   libSystem.B.dylib               0x35d5d198 nanosleep + 120
2   Foundation                      0x351e066a +[NSThread sleepForTimeInterval:] + 98
3   AppName                         0x00005b40 -[Syncer requestPump] (Syncer.m:116)
4   Foundation                      0x3515cb8a -[NSThread main] + 42
5   Foundation                      0x35155b90 __NSThread__main__ + 908
6   libSystem.B.dylib               0x35de5b44 _pthread_start + 364
7   libSystem.B.dylib               0x35dd77a4 thread_start + 0

Thread 4:
0   libSystem.B.dylib               0x35d5b3b0 mach_msg_trap + 20
1   libSystem.B.dylib               0x35d5d894 mach_msg + 60
2   CoreFoundation                  0x37521f7c __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x37521780 __CFRunLoopRun + 400
4   CoreFoundation                  0x37521504 CFRunLoopRunSpecific + 220
5   CoreFoundation                  0x37521412 CFRunLoopRunInMode + 54
6   Foundation                      0x3517ec4e +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] + 210
7   Foundation                      0x3515cb8a -[NSThread main] + 42
8   Foundation                      0x35155b90 __NSThread__main__ + 908
9   libSystem.B.dylib               0x35de5b44 _pthread_start + 364
10  libSystem.B.dylib               0x35dd77a4 thread_start + 0

Thread 5:
0   libSystem.B.dylib               0x35d848d8 select$DARWIN_EXTSN + 20
1   CoreFoundation                  0x3755aa34 __CFSocketManager + 356
2   libSystem.B.dylib               0x35de5b44 _pthread_start + 364
3   libSystem.B.dylib               0x35dd77a4 thread_start + 0

Thread 6:
0   libSystem.B.dylib               0x35de72fc __workq_kernreturn + 8
1   libSystem.B.dylib               0x35de6b50 _pthread_wqthread + 872
2   libSystem.B.dylib               0x35ddd2fc start_wqthread + 0

Thread 7:
0   libSystem.B.dylib               0x35de72fc __workq_kernreturn + 8
1   libSystem.B.dylib               0x35de6b50 _pthread_wqthread + 872
2   libSystem.B.dylib               0x35ddd2fc start_wqthread + 0

Thread 8:
0   libSystem.B.dylib               0x35de72fc __workq_kernreturn + 8
1   libSystem.B.dylib               0x35de6b50 _pthread_wqthread + 872
2   libSystem.B.dylib               0x35ddd2fc start_wqthread + 0

Thread 9 Crashed:
0   CoreFoundation                  0x374fec60 __CFBasicHashAddValue + 140
1   CoreFoundation                  0x374ff8fc CFBasicHashAddValue + 276
2   CoreFoundation                  0x3750410a CFSetAddValue + 90
3   CoreFoundation                  0x3752be2a -[__NSCFSet addObject:] + 34
4   CoreData                        0x34998002 -[NSManagedObjectContext(_NSInternalAdditions) _insertObjectWithGlobalID:globalID:] + 134
5   CoreData                        0x34997ec6 -[NSManagedObjectContext insertObject:] + 58
6   CoreData                        0x34979cbc -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] + 248
7   AppName                         0x00066512 -[SNServerChanges newItemWithJSONStringFor:withList:] (SNServerChanges.m:655)
8   AppName                         0x0006847a -[SNServerChanges requestDidFinishThread:] (SNServerChanges.m:282)
9   AppName                         0x00065b28 -[SNServerChanges makeChanges:] (SNServerChanges.m:41)
10  Foundation                      0x3515cb8a -[NSThread main] + 42
11  Foundation                      0x35155b90 __NSThread__main__ + 908
12  libSystem.B.dylib               0x35de5b44 _pthread_start + 364
13  libSystem.B.dylib               0x35dd77a4 thread_start + 0

Thread 9 crashed with ARM Thread State:
    r0: 0x0405ceb0    r1: 0x00000000      r2: 0x00000004      r3: 0x00000001
    r4: 0x0405ceb0    r5: 0x002e1c20      r6: 0x00000004      r7: 0x04468884
    r8: 0x00000001    r9: 0x044688ac     r10: 0x0405ceb0     r11: 0x002bf670
    ip: 0x00000000    sp: 0x04468870      lr: 0x34982145      pc: 0x374fec60
  cpsr: 0x20000010

我知道的一件事是,当我尝试向Core Data添加新对象时出现了问题。

这就是我设置核心数据的方式:

- (id) init {
    if (self = [super init]) {
        NSString* path = [[NSBundle mainBundle] pathForResource:@"Main210" ofType:@"mom"];

        NSURL* url = [NSURL fileURLWithPath:path];
        url = [[NSURL alloc] initFileURLWithPath:path];
        managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil];


        NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"data.bin"]];

        NSError *error = nil;

        sqlite3_enable_shared_cache(0); // from apple Bug ID# 8055992 
        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) 
        {
            NSLog(@"Error: %@ \n\n userInfo: %@ \n\n ", error, [error userInfo]);

            NSError *underlyingError = [[error userInfo] objectForKey:NSUnderlyingErrorKey];
            if (underlyingError) {
                 NSLog(@"Underlying error: %@ \n\n userInfo: %@ \n\n", underlyingError, [underlyingError userInfo]);
            }

            NSArray *detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
            for (NSError *detailError in detailedErrors) {
                 NSLog(@"Detailed error: %@ \n\n userInfo: %@ \n\n", detailError, [detailError userInfo]);
            }
            abort();
        }   


        if (persistentStoreCoordinator != nil) {
            managedObjectContext = [[NSManagedObjectContext alloc] init];
            [managedObjectContext setPersistentStoreCoordinator: persistentStoreCoordinator];
            NSUndoManager *undoManager = [[NSUndoManager alloc] init];
            [managedObjectContext setUndoManager:undoManager];
            [managedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
        }
    }
    return self;
}

导致崩溃的功能:

-(void) newItemWithJSONStringFor:(NSArray*) itemsArray withList:(Lista*) lista {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSNumber *databaseID;   
    if(LOG==1)NSLog(@"Lista : %@, itemsArrayCount: %d",[lista name], [itemsArray count]);
    for (int i=0; i<itemsArray.count; i++) {

        [NSThread sleepForTimeInterval:EACHITEMWAITTIME];
        NSDictionary* jsonItem = [itemsArray objectAtIndex:i];
        databaseID = [jsonItem valueForKey:@"Id"];  

        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[CoreDataHandler syncContext]];
        [request setEntity:entity];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"databaseID == %qi", [databaseID longLongValue]];
        [request setPredicate:predicate];
        NSError* error;
        NSMutableArray *mutableFetchResults = [[[CoreDataHandler syncContext] executeFetchRequest:request error:&error] mutableCopy];

        [request release];
        BOOL flag = YES; //will item be created?
        if (! (mutableFetchResults && [mutableFetchResults count] == 0) ) {
            flag = NO;
        }

        Item* item = [mutableFetchResults lastObject];

        if (!item) {
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[CoreDataHandler syncContext]];
            item = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:[CoreDataHandler syncContext]];
            item.databaseID = databaseID;
            item.databaseIDSet = [NSNumber numberWithBool:YES];
            item.lista = lista;

            [mutableFetchResults addObject:item];

        }
        else {
            if(LOG==1)NSLog(@"SNServerChanges - newItemWithJSONString - Modyfing");
        }



        if(LOG==1)NSLog(@"SNServerChanges - newItemWithJSONString - FillingItem");
        // if(LOG==1)NSLog(@"SNServerChanges - newItemWithJSONString - Making new");

        //Nazwa
        if ([jsonItem valueForKey:@"N"] != [NSNull null])
            item.name = [jsonItem valueForKey:@"N"];
        //Opis
        if ([jsonItem valueForKey:@"D"] != [NSNull null])
            item.desc = [jsonItem valueForKey:@"D"];
        //Ilosc
        if ([jsonItem valueForKey:@"Q"] != [NSNull null])
            item.quantity = [jsonItem valueForKey:@"Q"];
        //Jednostka
        if ([jsonItem valueForKey:@"U"] != [NSNull null])
            item.unit = [jsonItem valueForKey:@"U"];
        //Checked
        if ([jsonItem valueForKey:@"C"] != [NSNull null]){
            NSLog(@"SC New : %@, %d",[item fullName], [[item checked] boolValue]);
            if(![item.checkedChanged boolValue]){
                item.checked = [NSNumber numberWithBool:[[jsonItem valueForKey:@"C"] boolValue]];
            }
            else
                [SyncManager doNextSync];
        }
        //Pozycja
        if ([jsonItem valueForKey:@"O"] != [NSNull null])
            item.position = [NSNumber numberWithInt: [[jsonItem valueForKey:@"O"] intValue]];
        //Reklama
        if([jsonItem valueForKey:@"Adv"]!= nil && [jsonItem valueForKey:@"Adv"]!=[NSNull null]) {
            NSDictionary* temp = [jsonItem valueForKey:@"Adv"];
            NSString* tempString = [NSString stringWithFormat:@"%@",[temp valueForKey:@"T"]];
            item.advert = tempString;
        }
        //Cena
        if ( [jsonItem valueForKey:@"P"] != nil && [jsonItem valueForKey:@"P"]!=[NSNull null]) 
        {
            NSObject* obj = [jsonItem valueForKey:@"P"];
            item.price=[NSNumber numberWithDouble:[[jsonItem valueForKey:@"P"] doubleValue]];
        }
        if ([jsonItem valueForKey:@"Pe"] != [NSNull null])
            item.priceEstimated = [NSNumber numberWithBool:[[jsonItem valueForKey:@"Pe"] boolValue]];

        if ([jsonItem valueForKey:@"Ci"] != [NSNull null])
            item.categoryID = [NSNumber numberWithInt: [[jsonItem valueForKey:@"Ci"] intValue]];

        if (flag) {
            if ( [item.checked boolValue] )
                lista.checked = [NSNumber numberWithInt:[lista.checked intValue]+1];
            lista.count = [NSNumber numberWithInt:[lista.count intValue]+1];
        }
        // if(LOG==1)NSLog(@"SNServerChanges - newItemWithJSONString - Fetching");
        if(LOG==1)NSLog(@"SNServerChanges - newItemWithJSONString - AddingToItemCache");
        if(LOG==1)NSLog(@"\tItem : %@", [item name]);
        //[ItemCache processItem: item];

//      [(SNServerChanges*)[SNServerChanges sharedInstance]  performSelectorOnMainThread:@selector(saveCoreData) withObject:nil waitUntilDone:YES];

        if(LOG==1)NSLog(@"SNServerChanges - newItemWithJSONString - ItemAdded");

    }
    [pool drain];
}

2 个答案:

答案 0 :(得分:1)

我看不出你出错的地方(除非模型中没有Item实体 - 为什么不NSLog(@"%@",item);?)。我总是使用不同的调用来添加新对象,而不是显式分配NSManagedObject

[NSEntityDescription insertNewObjectForEntityForName:@"Item"
                     inManagedObjectContext:managedObjectContext];

不确定是否重要。

答案 1 :(得分:0)

该应用程序可能会崩溃:

item = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:[CoreDataHandler syncContext]];

您之前已从item开始定义Item,但现在您正在为其分配通用NSManagedObject。这可能是事故的原因。

相关问题