NSFetchedResultsController deallocated instance - iphone-sdk-3.0

Anyone ever encountered this ?
-[NSFetchedResultsController _restoreCachedSectionInfo]: message sent to deallocated instance
While performing fetch with performFetch: using NSFetchedResultsController instance.
I'm sure the NSFetchedResultsController is retained before performing the fetch.

That error is a hint that you are overreleasing something, probably the controller. Check your properties and count your retains and releases; there is any extra somewhere

Related

Serious application error. Model object getting NSFetchedResultsController message

I have a very odd error with Core Data. I have a Recipe class that has a many to many relationship to an Ingredient class.
When I try to add an Ingredient to the Recipe on a RecipeViewController, everything works fine until I save the context. Thats when things go horribly wrong.
I get an error on my RecipesTableViewController (which has a NSFetchedResultsController and implements NSFetchedResultsControllerDelegate). The
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
method gets called and then I get an exception:
CoreData: error: Serious application error. Exception was caught
during Core Data change processing. This is usually a bug within an
observer of NSManagedObjectContextObjectsDidChangeNotification.
-[KCGIngredient sectionOffset]: unrecognized selector sent to instance 0x7bf83d50 with userInfo (null)
I have no idea what this sectionOffset message is and couldn't find it in the documentation. This happens after NSFetchResultsController gets the _postprocessInsertedObjects: message.
I'm assuming this sectionOffset message was intended to an instance of NSFetchedResultsController, but I have no idea how it ended up being sent to a model object.
EDIT:
Actually, I checked it out and apparently sectionOffset is not a method of NSFetchedResultsController.
This test fails miserably:
XCTAssertTrue([NSFetchedResultsController instancesRespondToSelector:#selector(sectionOffset)]);
What on Earth is going on? I already tried everything,s o if you can shed some light, I'd really appreciate it.

Crash in CoreData while saving data in from a backgroundthread

I have a http request which is running in backgroundthread. As soon as the data arrives i am trying to save that into the coredata but it crashes randomly at many points with EXC_BAD_ACCESS error. Can somoeone point me to the right direction.
You are properly releasing an array already released. Try enabling NSZombie or check if you are calling release on an array you did not create using the alloc keyword

Accessing deleted objects in iCloud notification

I have an app set up much like the iCloudCoreDataRecipes sample (ie, using Core Data in conjunction with iCloud). In the app delegate, I observe the
NSPersistentStoreDidImportUbiquitousContentChangesNotification
When a notification arrives, I call
[context mergeChangesFromContextDidSaveNotification:note];
I have some additional processing I'd like to do when this notification is received but am having trouble using the objects identified by the NSManagedObjectID's present in the NSDeletedObjectsKey set.
NSSet *deletedObjects = [info objectForKey:NSDeletedObjectsKey];
for (NSManagedObjectID *oid in deletedObjects) {
NSManagedObject *obj = [context objectWithID:oid];
}
If I access any property on the obj, it is nil.
I then tried running the above code prior to calling mergeChangesFromContextDidSaveNotification:
When I did that, I was able, most of the time, to access the properties on the object. In some cases, I'd get an exception for unable to fulfill fault; the record was already deleted from the Core Data store.
I realized that accessing the deleted object's properties would work if the object had been loaded into the context some time prior to the notification arriving (ie, if the object was viewed/accessed within the app).
My problem is that I'd like to do some clean-up related to the deleted objects; my NSManagedObject's have a property that I'd like to read and then use to perform some work outside of Core Data related to that value.
What am I missing? Is it possible to do this?
You should probably look at
- (void)prepareForDeletion;
and override that in your NSManagedObject subclass.

objective-c : will this create a memory leak

If I have a code looked like this:
-(void) func {
ObjectA* A = [[ObjectA alloc]init];
[something doSomething:blah andDelegate: A];
}
Assuming the call of doSomething will make a http request call so the delegate will be called only when it received response from the server. In this case, there would be a delay.
Note: something is an instance variable of a class.
If I call 'func' twice, will the first initialized of A be leaked before it received the response on the delegate. Assuming there is a release operation when calling the delegate function when received the responsed.
The reason I thinking of this is because if second initialized of 'A' passed in to something as an delegate before the first delegate finished it's role. Will second initialized of 'A' replaced the first initialized of 'A'?
Yes, if you're not compiling with ARC, you have a leak. You're creating an object with alloc, which means that you own it, and you're not relinquishing that ownership by sending release. This is the core memory management rule for Cocoa.
It may be that the object, something, to which you're passing A, also needs to own A (in fact, it sounds like that is the case). If so, something should send retain to A and then send release when it no longer needs A.
Will second initialized of 'A' replaced the first initialized of 'A'?
Sort of. The name A is only valid inside this method. When you create an object and assign it to A, and then that name goes out of scope, you can't refer to the object anymore. That's what a leak is. When you run this method again, essentially a new name A is created and you assign another object to it.
something and func should be releasing A. something should also retain A when its used.

The optimal way to determine if NSManagedObject related attribute object exist

I got a log object retrieved by NSFetchedResultsController, it has an optional attribute that's an slide object. And it's currently faulted. So if I log log.slide, it will return a faulted object. How do I check if log actually have a slide object attached to it? I want to be able to use if(log.slide){...}
I tried to set the NSFetchRequest to [request setReturnsObjectsAsFaults:NO]; before hand it to NSFetchedResultsController. But it's still returning faulted objects. Thanks!
A fault is automatically fulfilled when you access it, so you can simply use your example if (log.slide) {...} and it will work like you expect it to, fulfilling the fault if necessary.