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.
Related
I am using RestKit with Coredata and fetching data from the server and displaying.
Now I am doing a post from the client and this object gets updated as part of the response that comes back from the server. This is where the problems starts.
I looked for the correct way to implement this and came across 2 main points.
MOCs should not be shared across threads
An object created in the MOC is not available in another thread without saving.
But i think since the record gets updated from server response, its no longer finding the orig object. I just dont knw what the right fix is.
Here is my code
1. Create local entity
NSEntityDescription *itemEntity = [NSEntityDescription entityForName:ENTITY_ITEM inManagedObjectContext:self.managedObjectContext];
Item *item = [[Item alloc]initWithEntity:itemEntity insertIntoManagedObjectContext:self.managedObjectContext];
// Set params on item here
// Then save it
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
DBGLog(#"Tried to save the new item but failed - error %#, %#", error, [error userInfo]);
}
// Then I make the RestKit call to post the item
// The server updates the item Id
[SharedManager postItem:item success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// successful case
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// failure case
}];
It looks like when its trying to make the response it doesnt find the object.
And i get this exception -
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x156c87b0 <x-coredata://A42ABF18-01B6-4D78-B81B-62D8B604EB52/Item/p6>''
*** First throw call stack:
(0x2f853f0b 0x39feace7 0x2f5b7fd1 0x2f61a655 0x2f6246a7 0x2f6326e5 0x2f632a95 0x2f63356f 0x3a4d3d3f 0x3a4d86c3 0x2f628e7b 0x2f633271 0x2f5c7f49 0x1c67fb 0x2f62b9cd 0x3a4d9b3b 0x3a4d3d3f 0x3a4d66c3 0x2f81e681 0x2f81cf4d 0x2f787769 0x2f78754b 0x346f46d3 0x320e6891 0x72561 0x3a4e8ab7)
libc++abi.dylib: terminating with uncaught exception of type _NSCoreDataException
If I dont do a "save" then I see Cocoa Error 133000 on 4S devices. So there is definitely something I am messing up.
Appreciate any insights!
Your comment is along the correct lines, but not the correct solution. The problem is that you only save the main thread context and the change doesn't get pushed up to the parent (the persistent context). So, instead of calling if (![self.managedObjectContext save:&error]) { you should be calling if (![self.managedObjectContext saveToPersistentStore:&error]) {
Sometimes when I call saveToPersistent on the RestKit MainQueueManagedObjectContext I get the following error and the app crashes.
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[UIBarButtonItem controllerWillChangeContent:]: unrecognized selector sent to instance 0x21e70300 with userInfo (null)
[UIBarButtonItem controllerWillChangeContent:]: unrecognized selector sent to instance 0x21e70300 with userInfo (null)
It seems that there are some kind of threading problem.
I save the context via a method like so:
- (BOOL)saveMainContext
{
__block BOOL contextSaved = NO;
[[[[CoreDataManager sharedInstance] objectStore] mainQueueManagedObjectContext] performBlockAndWait:^{
NSError *error = nil;
if ([[[[CoreDataManager sharedInstance] objectStore] mainQueueManagedObjectContext] saveToPersistentStore:&error]) {
contextSaved = YES;
}
}];
return contextSaved;
}
It crashes on the saveToPersistent method with EXC_BAD_ACCESS.
How can I fix this and what could be the reason for my NSFetchresultscontroller calling a method on a UIBarButtonItem?
Your saveMainContext method should only be called from the main thread and should simply be implemented as:
- (BOOL)saveMainContext
{
NSError *error = nil;
if ([[[[CoreDataManager sharedInstance] objectStore] mainQueueManagedObjectContext] saveToPersistentStore:&error]) {
contextSaved = YES;
} else {
NSLog(#"Save error: %#", error);
}
return contextSaved;
}
Calling an instance of UIBarButtonItem suggests that you have a memory management issue in that something which is observing the context save is not removing itself before it is deallocated. Check all of your observers.
I'd like to delete a core data object by fetched the object first, so
in FetchObject.m
- (void) actionDelete {
AModel *aModel = [[aModel alloc] init];
AObj *aObj = [aModel readDataWithAttributeName:#"keyword" attributeValue:#"value"];
[aModel deleteObject:aObj];
}
aObj did fetch and obtain.
in AModel.m
- (void)deleteObject:(AObj *)aObj
{
[appDelegate.managedObjectContext delete:aObj];
NSError *error;
if (![appDelegate.managedObjectContext save:&error]) {
NSLog(#"Error: %#", [error description]);
}
}
But, when I test it, here came out an error
-[NSManagedObjectContext delete:]: unrecognized selector sent to instance 0xa43ece0
After searching the solution a bit, seems like the target has been release before deleteObject.
Is there any way to solve the problem?
The following code is causing the issue:
[appDelegate.managedObjectContext delete:aObj];
Replace it with:
[appDelegate.managedObjectContext deleteObject:aObj];
NSManagedObjectContext doesn't have a delete method, it only has a deleteObject method.
- (void)deleteObject:(NSManagedObject *)object
Parameters
object
A managed object.
Discussion
When changes are committed, object will be removed from the uniquing
tables. If object has not yet been saved to a persistent store, it is
simply removed from the receiver.
I'm setting up a very simple NSIncrementalStore example using AFIncrementalStore.
The idea is to setup a NSManagedObjectContext in the AppDelegate (using the ordinary template provided by Apple, with changes for my IncrementalStore), do a fetch with no predicate or sort descriptor and NSLog the one fetched entity object.
Everything works great until I ask for any entity attribute. It crashes with the following message:
2013-07-22 16:34:46.544 AgendaWithAFIncrementalStore[82315:c07] -[_NSObjectID_id_0 eventoId]: unrecognized selector sent to instance 0x838b060
2013-07-22 16:34:46.545 AgendaWithAFIncrementalStore[82315:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_id_0 eventoId]: unrecognized selector sent to instance 0x838b060'
My xcdatamodeld is correctly setted up. The NSManagedObject class is generated and imported on the delegate. When I do a breakpoint before the NSLog I can see the fetched objects IDs. The webservice is giving me back the correct data.
My AppDelegate code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[self.window makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(remoteFetchHappened:) name:AFIncrementalStoreContextDidFetchRemoteValues object:self.managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:#"Agenda" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = entityDescription;
fetchRequest.predicate = nil;
NSError *error;
[self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
return YES;
}
// Handle the notification posted when the webservice returns objects
- (void)remoteFetchHappened:(NSNotification *)aNotification
{
NSArray *fetchResult = [[aNotification userInfo] objectForKey:#"AFIncrementalStoreFetchedObjectIDs"];
Agenda *agenda = (Agenda *)[fetchResult lastObject];
// THIS IS WHERE IT BREAKS...
NSLog(#"Agenda: %#", agenda.eventoId);
}
Any ideas on how to make this piece of code return the attribute I'm asking for?
AFNetworking is giving you managed object IDs, that is, instances of NSManagedObjectID. You can't look up managed object property values on that-- you have to get the managed object for the ID first. That's what _NSObjectID_id_0 means in the error message-- you're trying to get
eventoId on an NSManagedObjectID, and it has no idea what that is.
You get the managed object by looking it up in the managed object context. Something like
NSError *error = nil;
NSManagedObject *myObject = [context existingObjectWithID:objectID error:error];
if (myObject != nil) {
// look up attribute values on myObject
}
I have an iOS program using Core Data backing onto DynamoDB using ARC.
In it I have a data model with the following class declared:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Track : NSManagedObject
#property (nonatomic, retain) NSString * trackId;
#end
-------------------------------------------------
#import "Track.h"
#implementation Track
#dynamic trackId;
#end
I then have a class creating an instance of the ManagedObject and persisting it like so:
NSString *trackId = #"ABC123";
Track *track = (Track*)[NSEntityDescription insertNewObjectForEntityForName:#"Track" inManagedObjectContext:context];
track.trackId=trackId;
NSError *error;
if (![appDelegate.managedObjectContext save:&error])
{
NSLog(#"error: %#", error);
}
This works as expected. However, if I implement the same using a switch to alter the value of trackId as below:
for (int i=0; i<1; i++) {
NSString *trackId;
switch (i) {
case 0:
trackId = #"XYZ789";
break;
}
Track *track = (Track*)[NSEntityDescription insertNewObjectForEntityForName:#"Track" inManagedObjectContext:context];
track.trackId=trackId;
}
NSError *error;
if (![appDelegate.managedObjectContext save:&error])
{
NSLog(#"error: %#", error);
}
then I get the following exception:
2013-01-16 14:44:00.013 DjTest2[2351:c07] XYZ789
2013-01-16 14:44:00.014 DjTest2[2351:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
*** First throw call stack:
(0x183e012 0x1663e7e 0x18c10de 0x107c9bf 0x39799 0x1ff5e4 0xc7831 0xf533c 0x3411 0x68c817 0x68c882 0x5dba25 0x5dbdbf 0x5dbf55 0x5e4f67 0x5a8fcc 0x5a9fab 0x5bb315 0x5bc24b 0x5adcf8 0x268bdf9 0x268bad0 0x17b3bf5 0x17b3962 0x17e4bb6 0x17e3f44 0x17e3e1b 0x5a97da 0x5ab65c 0x258d 0x24b5)
libc++abi.dylib: terminate called throwing an exception
I assume there is some issue I'm not aware of regarding variable scope / retention using ARC where the NSString literal is created within the scope of a switch, but I can't find any reference to it.
Does anyone know what the root cause to this is and how you would efficiently deal with persisting a large number of objects without being able to perform such manipulation?
Note: I have mentioned backing on to DynamoDB for completeness, but this exception is raised before any attempt to call to AWS is made.
I'm afraid I have lead us astray with my code example above which I updated for clarity when entering on SO.
The actual problem is not that it is within the switch statement, rather that some of the real keys I'm using have spaces in them which I assume is then being restricted by Core Data given that DynamoDB has no such restriction.