' - (NSString *)description'NSManagedObject的to-many关系的方法导致崩溃

时间:2014-03-17 08:40:48

标签: ios objective-c core-data many-to-many nsmanagedobject

我的应用程序使用CoreData发生了非常奇怪的问题。

实体A与实体B具有To-Many关系,反向关系也是To-many。关系是双向可选的,删除规则是Nullify。

现在插入的记录没有B关系,NSManagedObjectContext保存没有错误。然后我获取一个A对象并尝试NSLog它最终会出现异常:

-[__NSCFNumber length]: unrecognized selector sent to instance

我没有覆盖- (NSString *)description方法,我在A类中没有任何自定义getter,所以我不知道它为什么抛出这个异常。有趣的是,当我在调试器中“打印[aObject.bRelationship allObjects]对象”时,它会打印一个空数组,但是[aObject bRelationship]会抛出该异常。

我尝试过使用-com.apple.CoreData.SQLDebug 1,它并没有真正说明任何有用的信息。

有什么想法吗?

编辑1:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [super application:application didFinishLaunchingWithOptions:launchOptions];


    [[SyncManager shared] setupCoreDataStack];

    @try {
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
        request.predicate = [NSPredicate predicateWithFormat:@"title != nil"];
        NSError *error;
        MyEntity *object = [[[SyncManager shared].context executeFetchRequest:request error:&error] lastObject];

        NSLog(@"object: %@, error: %@", object, error);
        NSLog(@"relationship.allObject: %@", object.collections.allObjects);
        NSLog(@"relationship: %@", object.collections);
    }
    @catch (NSException *exception) {
        NSLog(@"exception : %@", exception);
    }
    @finally {
        NSLog(@"Stack trace : %@",[NSThread callStackSymbols]);
    }
}

输出:

[45502:60b] object: <MyEntity: 0x10a7e360> (entity: MyEntity; id: 0x10a6f380 <x-coredata://457EDA6A-5890-4CC8-8997-A734E89D68C0/MyEntity/p106> ; data: <fault>), error: (null)
[45502:60b] relationship.allObject: (
    )
[45502:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x10862f40
[45502:60b] exception : -[__NSCFNumber length]: unrecognized selector sent to instance 0x10862f40
[45502:60b] Stack trace : (
        0   MyApp                               0x001e3bb3 -[AppDelegate application:didFinishLaunchingWithOptions:] + 1603
        1   UIKit                               0x04ae314f -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
        2   UIKit                               0x04ae3aa1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1810
        3   UIKit                               0x04ae8667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
        4   UIKit                               0x04afcf92 -[UIApplication handleEvent:withNewEvent:] + 3517
        5   UIKit                               0x04afd555 -[UIApplication sendEvent:] + 85
        6   UIKit                               0x04aea250 _UIApplicationHandleEvent + 683
        7   GraphicsServices                    0x0833af02 _PurpleEventCallback + 776
        8   GraphicsServices                    0x0833aa0d PurpleEventCallback + 46
        9   CoreFoundation                      0x06fb9ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
        10  CoreFoundation                      0x06fb99db __CFRunLoopDoSource1 + 523
        11  CoreFoundation                      0x06fe468c __CFRunLoopRun + 2156
        12  CoreFoundation                      0x06fe39d3 CFRunLoopRunSpecific + 467
        13  CoreFoundation                      0x06fe37eb CFRunLoopRunInMode + 123
        14  UIKit                               0x04ae7d9c -[UIApplication _run] + 840
        15  UIKit                               0x04ae9f9b UIApplicationMain + 1225
        16  MyApp                               0x0000897c main + 76
        17  libdyld.dylib                       0x07587701 start + 1
        18  ???                                 0x00000001 0x0 + 1
    )

MyEntity.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class B;

@interface MyEntity : NSManagedObject

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSSet *collections;
@end

@interface MyEntity (CoreDataGeneratedAccessors)

- (void)addCollectionsObject:(B *)value;
- (void)removeCollectionsObject:(B *)value;
- (void)addCollections:(NSSet *)values;
- (void)removeCollections:(NSSet *)values;
@end

0 个答案:

没有答案