CoreData:实体中的可转换类型|获取失败

时间:2016-06-10 06:15:11

标签: ios objective-c core-data

我有一个实体; UserDefaults具有属性; user_contacts,即Contacts(NSManagedObject模型)列表。我把它保存在我的.sqlite中。然而,当我试图找回它时,我遇到了可怕的崩溃。我使用此代码来设置和获取UserDefaults;

地点:

for (int k = 0; k < allContactCopy.count; k++)
{
    Contact *contact = [allContactCopy objectAtIndex:k];
    Contact *contactAllData = [Contact getContactForNumber:contact.number];
    contactAllData.reg = NO;
    contactAllData.iD = contact.iD;
    contactAllData.name = contact.name;
    contactAllData.valTag = contact.valTag;
    [contactsAllWithNonRegData addObject:contactAllData];
}

UserDefaults *userD = (UserDefaults*)[[TTDatabaseManager sharedTTDatabaseManager] insertNewMOForModelName:@"UserDefaults"];
userD.user_contacts = [NSArray arrayWithArray:contactsAllWithNonRegData];
[[TTDatabaseManager sharedTTDatabaseManager] saveDataInManagedContext];

后续调用方法是;

- (NSManagedObject*)insertNewMOForModelName:(NSString*)modelName
{
    NSManagedObject *objectManaged = [NSEntityDescription insertNewObjectForEntityForName:modelName
                                                                   inManagedObjectContext:self.managedObjectContext];
    return objectManaged;
}

UserDefaults *result = (UserDefaults*)[[TTDatabaseManager sharedTTDatabaseManager] fetchMODataWithModelName:@"UserDefaults" attributeNameToSortBy:@"user_contacts" ascending:NO predicate:nil];

它的后续调用方法是;

- (NSManagedObject*)fetchMODataWithModelName:(NSString *)modelName
                   attributeNameToSortBy:(NSString *)attribute
                               ascending:(BOOL)ascending
                               predicate:(NSPredicate *)predicate
{
    NSManagedObject *managedObject = nil;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:modelName
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:attribute
                                                                   ascending:ascending];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setPredicate:predicate];

    NSError *error = nil;
    NSArray *fetchResults = [self.managedObjectContext executeFetchRequest:fetchRequest
                                                                     error:&error];//IT CRASHES HERE

    if (fetchResults == nil)
    {
        // Handle the error.
        NSLog(@"executeFetchRequest failed with error: %@", [error localizedDescription]);
    }
    else if(fetchResults && [fetchResults count] > 0)
    {
        managedObject = [fetchResults objectAtIndex:0];
    }

    return managedObject;
}

self.managedObjectContext执行请求的地方崩溃了。我现在已经撞了我一天超过一天,但却找不到为什么会发生这种情况。非常感谢您的帮助。崩溃的细节是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Contact initWithCoder:]: unrecognized selector sent to instance 0x7f9d61c708e0'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010bfb8d85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010b6a6deb objc_exception_throw + 48
2   Foundation                          0x0000000109584bda -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 0
3   Foundation                          0x0000000109584dee -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 532
4   Foundation                          0x00000001094e0859 _decodeObjectBinary + 3589
5   Foundation                          0x00000001094df94d _decodeObject + 281
6   Foundation                          0x0000000109502111 +[NSKeyedUnarchiver unarchiveObjectWithData:] + 89
7   CoreData                            0x000000010bb17b63 _prepareResultsFromResultSet + 3411
8   CoreData                            0x000000010bb14fdd newFetchedRowsForFetchPlan_MT + 3293
9   CoreData                            0x000000010bb01bac -[NSSQLCore objectsForFetchRequest:inContext:] + 524
10  CoreData                            0x000000010bb01549 -[NSSQLCore executeRequest:withContext:error:] + 377
11  CoreData                            0x000000010bbe6eaf __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3311
12  CoreData                            0x000000010bbf04dd gutsOfBlockToNSPersistentStoreCoordinatorPerform + 189
13  libdispatch.dylib                   0x000000010c7693eb _dispatch_client_callout + 8
14  libdispatch.dylib                   0x000000010c74def5 _dispatch_barrier_sync_f_invoke + 393
15  CoreData                            0x000000010bbe15d5 _perform + 197
16  CoreData                            0x000000010bb011b4 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 500
17  CoreData                            0x000000010baff973 -[NSManagedObjectContext executeFetchRequest:error:] + 579
18  tellotalk                           0x0000000107992b24 -[TTDatabaseManager fetchMODataWithModelName:attributeNameToSortBy:ascending:predicate:] + 532
19  tellotalk                           0x00000001079a9b1b -[ChatListViewController viewWillAppear:] + 459
20  UIKit                               0x0000000109da82bd -[UIViewController _setViewAppearState:isAnimating:] + 710
21  UIKit                               0x0000000109da8958 -[UIViewController __viewWillAppear:] + 149
22  UIKit                               0x0000000109d75de7 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 934
23  UIKit                               0x0000000109c13f62 _runAfterCACommitDeferredBlocks + 317
24  UIKit                               0x0000000109c27e4c _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
25  UIKit                               0x0000000109c34147 _afterCACommitHandler + 90
26  CoreFoundation                      0x000000010beddc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
27  CoreFoundation                      0x000000010beddba7 __CFRunLoopDoObservers + 391
28  CoreFoundation                      0x000000010bed37fb __CFRunLoopRun + 1147
29  CoreFoundation                      0x000000010bed30f8 CFRunLoopRunSpecific + 488
30  GraphicsServices                    0x000000010e5adad2 GSEventRunModal + 161
31  UIKit                               0x0000000109c07f09 UIApplicationMain + 171
32  tellotalk                           0x00000001079904ff main + 111
33  libdyld.dylib                       0x000000010c79d92d start + 1
34  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:2)

你的方法都是错的,尤其是问题在于

userD.user_contacts = [NSArray arrayWithArray:contactsAllWithNonRegData];

因为你已经使属性转换能够NSCoding用于将该数组更改为要存储在SQLite中的数据。显然,我的归档过程无法在上下文中创建适当的托管对象,因此您可能会崩溃。

你需要摆脱数组并使用关系而不是属性。