Reputation: 1018
I have an Entity; UserDefaults
which has an Attribute; user_contacts
, i.e is a list of Contacts
(NSManagedObject models). I save it comfortably in my .sqlite. However when I try to retrieve it, I get a horrible crash. I use this code to set and fetch my UserDefaults
;
SETTING:
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];
Consequent calling method is;
- (NSManagedObject*)insertNewMOForModelName:(NSString*)modelName
{
NSManagedObject *objectManaged = [NSEntityDescription insertNewObjectForEntityForName:modelName
inManagedObjectContext:self.managedObjectContext];
return objectManaged;
}
FETCHING:
UserDefaults *result = (UserDefaults*)[[TTDatabaseManager sharedTTDatabaseManager] fetchMODataWithModelName:@"UserDefaults" attributeNameToSortBy:@"user_contacts" ascending:NO predicate:nil];
And its in consequent calling method is;
- (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;
}
It crashes where self.managedObjectContext
executes the request. I have been ramming my head for over a day now and cannot find why its happening so. Your help is appreciated. Crash details are:
*** 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
Upvotes: 3
Views: 584
Reputation: 119031
Your approach is all wrong, in particular the problem line is
userD.user_contacts = [NSArray arrayWithArray:contactsAllWithNonRegData];
Because you have made the attribute transform able NSCoding
is used to change that array into data to store in the SQLite. Obviously the I archiving process can't create a proper managed object in a context so you get this crash.
You need to get rid of the array and use a relationship instead of an attribute.
Upvotes: 2