[NSManagedObjectContext delete:]: unrecognized selector sent to instance - ios

core data getting on my nerves. i' m deleting old data and want to insert new data received from server.
now the deleting part gives a "sigabort":
-[NSManagedObjectContext delete:]: unrecognized selector sent to instance 0x522f550
2013-09-27 14:05:56.592 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObjectContext delete:]: unrecognized selector sent to instance 0x522f550'
* First throw call stack:
(0x320f82a3 0x39d4797f 0x320fbe07 0x320fa531 0x32051f68 0x1b6c53 0x1868e5 0x3a15f11f 0x3a16d259 0x3a16d3b9 0x3a193a11 0x3a1938a4)
libc++abi.dylib: terminate called throwing an exception
deleting happens in background and nsmanagedobjectcontext is a privet concurrency type
i printed the pointer for context and object context
po context NSManagedObjectContext: 0x522f550
and
po tmpCon.managedObjectContext NSManagedObjectContext: 0x522f550>
and the code for deleting is:
NSError *errorAllCons = nil;
NSFetchRequest *allevents = [[NSFetchRequest alloc] init];
[allevents setEntity:[NSEntityDescription entityForName:#"TEventContact" inManagedObjectContext:context]];
NSArray *allCons = [context executeFetchRequest:allevents error:&errorAllCons];
for (TEventContact *tmpCon in allCons)
{
[context delete:tmpCon];
}
and it crashes on [context delete:tmpCon];
can someone let me know what am i doing wrong?

Use
[context deleteObject:tmpCon];
It will solve the problem

do it like this for deleting an object permanently
for (TEventContact *tmpCon in allCons)
{
[context deleteObject:tmpCon] /// for deleting object in context
}
[context save]; /// to reflect changes in database you need to save that context
without saving the context it doesn't reflect on data base and that may cause DIRTY_READ for another context

Related

Core Data NSRangeException Crash in multi threaded block

I have a core data structure where contacts are parsed from server and associated with an Account entity.To create crash I:
1. login with user A and create a contact
2. login with user B and crash occurs.
Obviously there is an empty NSArray causing this crash.
The crash occurs while looping through each of the contacts in the server data with performBlock:
NSManagedObjectContext *mainMOC = self.managedObjectContext;
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
//NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[moc setParentContext:mainMOC];
[moc setUndoManager:nil];
BCRAccount *account2 = (BCRAccount*)[moc objectWithID:[self.loggedInAccount objectID]];
//[moc performBlockAndWait:^{
[moc performBlock:^{
//parse each contact
Also i call a fetch request for each contact in a separate method that references the child managed object:
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:[BCRContact entityName]];
NSManagedObjectContext *managedObjectContext = childmoc;
NSArray *fectchresult = [managedObjectContext executeFetchRequest:fetch error:&error];
I'm wondering if it is a to-many or to-one relationship problem. There must be some left over data from the first login that is contributing to the 2nd login crash.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArray0 objectAtIndex:]: index 15 beyond bounds for empty NSArray'
*** First throw call stack:
(0x18be921c0 0x18a8cc55c 0x18bdfd3dc 0x10024c154 0x1920301b4 0x1920303e4 0x19201d98c 0x192035448 0x191dd17e8 0x191cea220 0x18f1aa188 0x18f19ee64 0x18f19ed24 0x18f11b7ec 0x18f142c58 0x18f143678 0x18be3f7dc 0x18be3d40c 0x18be3d89c 0x18bd6c048 0x18d7f2198 0x191d582fc 0x191d53034 0x10008e578 0x18ad505b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
lastly before all of this parsing happens i see this error as well when a separate service is called:
2016-11-11 12:22:34.660266 AtEventUniversal[2223:781261] invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.
Thanks for any tips on this mess.
In a situation where you dont know whats causing the crash, you should use Exception Breakpoint... and Symbolic breakpoint... to catch the exception.

Unrecognized selector - save to Core Data

Help please to solve the problem - I do not understand why there is an error - unrecognized selector send to instance.
The error occurs when this code in AppDelegate is executed:
TMMonth *m = [NSEntityDescription insertNewObjectForEntityForName:#"TMMonth" inManagedObjectContext:self.managedObjectContext];
m.paymentMonth=#"month";
[self.managedObjectContext save:nil];
reason: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TMMonth name]: unrecognized selector sent to instance 0x7f993bdbbb00'
But in my code there is no selector 'name' in TMMonth class.
https://github.com/maks-84/paymentApp.git
error occurs when execute code in AppDelegate
TMMonth *m = [NSEntityDescription insertNewObjectForEntityForName:#"TMMonth" inManagedObjectContext:self.managedObjectContext];
m.paymentMonth=#"month";
[self.managedObjectContext save:nil];
reason: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TMMonth name]: unrecognized selector sent to instance 0x7f993bdbbb00'
But in my code no selector 'name' in TMMonth class

Core Data [Entity length]:

I'm working on an app and it's my first time using Core Data and everything was going smooth until I hit a rock.
Here is my stack trace
2015-07-20 23:39:41.030 Small Things[290:21425] -[Entity1 length]: unrecognized selector sent to instance 0x13769ecb0
2015-07-20 23:39:41.031 Small Things[290:21425] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Entity1 length]: unrecognized selector sent to instance 0x13769ecb0'
*** First throw call stack:
(0x184eaff94 0x199853f2c 0x184eb6cac 0x184eb3c4c 0x184db725c 0x184d97798 0x18a4cce20 0x10005f5f0 0x100063730 0x18a7fdb90 0x18a7fdce8 0x18a7ee890 0x18a802910 0x18a5abe78 0x18a4be4fc 0x189cfad04 0x189cf5920 0x189cf57e0 0x189cf4e80 0x189cf4bd4 0x189cee34c 0x184e66f44 0x184e64cbc 0x184e650ec 0x184d91800 0x18fe1f170 0x18a52a5e8 0x10005f4a0 0x19a08e8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
and here is the code that caused the problem:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//Fetch data from persistence
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Entity1"];
NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] initWithEntityName:#"Entity2"];
self.arr1 = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]mutableCopy];
self.arr2 = [[managedObjectContext executeFetchRequest:fetchRequest2 error:nil]mutableCopy];
[self.tableView reloadData];
}
I really don't get what's wrong.
Thanks in advance
Usually, this error is about the entity name you're using is not kind of NSString, so the code believes that you are using some type else (not NSString).
But it could be also, that is the model associated with the context’s persistent store coordinator does not contain an entity named Entity1.

CoreData: error: Serious application error. Exception was caught during Core Data change processing

Hi I'm getting crash,
when i'm trying to insert 1000 records into db in back ground, i'm getting following exception:
CoreData: error: Serious application error. Exception was caught during Core Data change processing.
This is usually a bug within an observer of
NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)2013-11-19 09:41:19.587 3pTalk[7487:907]
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
I used the code for inserting objexts
dispatch_queue_t myBackgroundQ = dispatch_queue_create("com.sample.addressbook", NULL);
// Could also get a global queue; in this case, don't release it below.
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
dispatch_after(delay, myBackgroundQ, ^(void){
[self userAddressbook];
});
dispatch_release(myBackgroundQ);
[self performSelectorOnMainThread:#selector(startSyncLoader) withObject:nil waitUntilDone:YES];
Dont access same Database(Persistent Store Coordinator) from 2 threads(Main,Background) simultaneously with one context. This is not recommended. it causes App Crash.
create NSManagedContext Object and set persistentstoreCoordinator.
dispatch_queue_t request_queue = dispatch_queue_create("com.xxx.ScsMethod", NULL);
dispatch_async(request_queue, ^{
NSPersistentStoreCoordinator *mainThreadContextStoreCoordinator = [context persistentStoreCoordinator]; //
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; //
[context setPersistentStoreCoordinator:mainThreadContextStoreCoordinator];}
Are you accessing the database from two threads simultaneously?, using same context? That may be the reason. see this question
Problems Adding to NSMutableArray: attempt to insert nil object at 10

NSKeyedArchiver deallocated without having had -finishEncoding called on it

In core data I've setup a simple entity called GolferEntity that contains simply a golferObject (transformable type) and playerId (string).
- (void)addOrUpdateGolfer:(GolferObject *)feedObj
{
NSLog(#"In add or update Feed");
// get reference to local (stored) golfer item, create it if needed
Golfer *localGolfer = [self golferForId:[feedObj PlayerId]];
if (localGolfer == nil) {
localGolfer = (Golfer *)[NSEntityDescription insertNewObjectForEntityForName:#"GolferEntity" inManagedObjectContext:[self managedObjectContext]];
[localGolfer setPlayerId:[feedObj PlayerId]];
}
// set folder fields
[localGolfer setGolferObj:feedObj];
// apply update
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(#"%#", [error localizedDescription]);
// return nil;
}
NSLog(#"successfully saved user: %#", [feedObj PlayerId]);
//return localFolder;
}
The code is giving me a runtime error which reads:
* -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GolferObject encodeWithCoder:]: unrecognized selector sent to instance 0x4b7520'
-- No where in my code have i alloc'd NSKeyedArchiver so I'm assuming this is something that done by core data? also my GolferObject does not have an encodeWithCoder method? I do not know where this is coming from?
I needed to encode before submitting to my custom object in CoreData. I needed to use the - (void)encodeWithCoder:(NSCoder *)encoder; and - (id)initWithCoder:(NSCoder *)decoder; methods.

Resources