Core Data Crash in the subclass of NSManagedObject

APPNameCoreDataEvent is the subclass of NSManagedObject. The crash happend in the property of APPNAMEEvent. What's wrong with it ??

Is there something that I missuse??


@class APPNAMECoreDataHeader;
@protocol APPNAMEPersistent
@property (nonatomic,strong) NSObject *model;
@end

@interface APPNameCoreDataEvent : NSManagedObject <APPNAMEEvent,APPNAMEPersistent>
/
*  subclass of NSManagedObject
*/
@property (nonatomic, retain) APPNAMECoreDataHeader * header;
@property (nonatomic,strong) APPNAMEEvent *model;
@end


Here's the Crash Log:


Thread 36 Crashed:
0   CoreData                            snapshot_get_value_as_object + 68
1   CoreData                            _PF_Handler_Public_GetProperty + 83
2   APPName                            -[APPNameCoreDataEvent model] (APPNameCoreDataEvent.m:35)
3   APPName                            __54-[APPNameCoreDataManager removeHistoryAnalytics:]_block_invoke (APPNameCoreDataManager71)
4   CoreData                            developerSubmittedBlockToNSManagedObjectContextPerform + 179
5   libdispatch.dylib                   _dispatch_queue_drain + 1763
6   libdispatch.dylib                   _dispatch_queue_invoke + 283
7   libdispatch.dylib                   _dispatch_root_queue_drain + 1573
8   libdispatch.dylib                   _dispatch_worker_thread3 + 95
9   libsystem_pthread.dylib             _pthread_wqthread + 1025
10  libsystem_pthread.dylib             start_wqthread + 8

What's your code surrounding APPNameCoreDataEvent.m line 35 doing? And what's APPNameCoreDataManager removeHistoryAnalytics: doing?


A stack trace and your header file aren't enough information to go on. You need to supply excerpts from the implemention file.

The surrounding APPNameCoreDataEvent.m:35 is the impelmentaion of model.

There is the code


- (APPNameCoreDataEvent *)model
{
    APPNameCoreDataEvent *model = [[APPNameCoreDataEvent alloc] init];
    for (NSString *key in [APPNameCoreDataEvent iterate_propertyNames]) {
        [model setValue:[self valueForKey:key] forKey:key];
    }
    return model;
}


While the impelmentation of removeHistoryAnalytics is below


- (void)removeHistoryAnalytics:(void (^)(APPAnalyticsHeader *header, NSSet *events))analytics
{
    [self.context performBlock:^{
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"APPAnalyticsCoreDataHeader"];
        [fetchRequest setFetchBatchSize:3];
        NSArray *headers = [self.context executeFetchRequest:fetchRequest error:nil];
        for (APPAnalyticsCoreDataHeader *header in headers) {
            if ([header.objectID.URIRepresentation isEqual:_headerURL]) {
                continue;
            }
            NSMutableSet *events = [NSMutableSet setWithCapacity:header.events.count];
            for (APPNameCoreDataEvent *event in header.events) {
                [events addObject:event.model];
            }
            if (analytics) {
                analytics(header.model, events);
            }
            [self.context deleteObject:header];
        }
        NSError *error = nil;
        [self.context save:&error];
     }];
}
  1. (APPNameCoreDataEvent *)model
  2. {
  3. APPNameCoreDataEvent *model = [[APPNameCoreDataEvent alloc] init];
  4. for (NSString *key in [APPNameCoreDataEvent iterate_propertyNames]) {
  5. [model setValue:[self valueForKey:key] forKey:key];
  6. }
  7. return model;
  8. }


If APPNameCoreDataEvent is a subclass of NSManagedObject, then the line [[APPNameCoreDataEvent alloc] init] and then set values on the object is part of the problem. In order to work properly, a Core Data object has to exist in a managed object context before you get and set attribute values on it.


Why are you trying to make copies of the managed objects like that?

Core Data Crash in the subclass of NSManagedObject
 
 
Q