First off I have to say this website and its members are amazing in their responses. Most helpful. thank you.
Secondly, I am troubleshooting the following error while troubleshooting an iPhone/iPad app that I am working on:
NSFetchRequest autoreleased with no pool in place - just leaking.
the culprit code seems to be
NSFetchRequest *request = [[NSFetchRequest alloc] init];
found in this method
-(NSArray *)searchDatabase:(int)uniqueID withPredicate:(NSString *)pred{
NSLog(#": DetailViewController/searchDatabase() method...executing. ");
NSLog(#": DetailViewController/searchDatabase() pred = %#",pred); // returns ParentID
NSError *error = nil;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Directory" inManagedObjectContext:self.managedObjectContext];
//: moved above line
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"%#=%#", pred, [NSNumber numberWithInt:uniqueID]]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setReturnsObjectsAsFaults:NO];
[request setEntity:entity];
[request setPredicate:predicate];
//: get the session and update the record with the time it ended
NSArray *mutableFetchResults = [self.managedObjectContext executeFetchRequest:request error:&error];
NSLog(#": DetailViewController/searchDatabase() mutableFetchResults = %#",mutableFetchResults);
[request release];
[error release];
NSLog(#": DetailViewController/searchDatabase() method...terminating.");
return mutableFetchResults;
}
I am not sure if the release statements are properly set up, but even still I am unsure while the error shows up in the first place. I am a little new to objective c, and at compile time the application did not indicate any errors (the message shows up at runtime).
If anyone can help me out with this, it would be most appreciated.
thanks
Edward
Related
I want to clear all the data by fetch all records and delete them one by one:
- (void) clear{
for (Program *program in [self getAllProgram]){
[managedObjectContext deleteObject:program];
}
if (![managedObjectContext save:&error])
{
NSLog(#"Problem deleting program: %#", [error localizedDescription]);
}
}
- (NSArray *)getAllProgram{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Program"inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat: #"freq = %#", self.freq];
[request setPredicate:predicate];
// Order the events by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"sid" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
return [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
}
The error is
CoreData: error: Serious application error. Exception was caught
during Core Data change processing. This is usually a bug within an
observer of NSManagedObjectContextObjectsDidChangeNotification.
-[__NSCFSet removeAllObjects]: mutating method sent to immutable object with userInfo (null)
Who can tell me the reason or tell me the one good practice ? thanks!
I suppose you have a fetched results controller somewhere. Set that controller's delegate to nil during the delete. I usually actually set the whole FRC to nil and update my whole data display after a large data change.
If that is not feasible, handle this case in the notification handler, like
if (!userInfo) return;
The static analyzer keeps telling me that I have a +1 retain count for my request object and instruments tells me there is a leak there. However, no matter where I try to release it, it keeps crashing my app. It's the same with NSPredicate object. Please do help, I'm trying to meet a deadline.
// Fetch Requests
// Method that returns an array of NSManagedObjects in the managedObjectContext with a predicate of who ordered
- (NSArray *)fetchDataWithEntity:(NSString *)entity andSortKey:(NSString *)key andPerson:(Person *)whoOrdered
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entity
inManagedObjectContext:managedObjectContext];
request.entity = entityDescription;
// Handling Sorting
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:YES
selector:#selector(compare:)];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
// Handling Predicate
if (whoOrdered) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%# == whoOrdered", whoOrdered];
[request setPredicate:predicate];
}
NSError *error = nil;
NSArray *mutableFetchResults = [managedObjectContext executeFetchRequest:request error:&error];
//[request release];
[error release];
if (mutableFetchResults == nil) {
// Handle the error
}
return mutableFetchResults;
}
There is no need to release the error object since you are not the owner of it and you should release the request object just before returning the mutableFetchResults object since you are creating it using alloc and thus are the owner of it ...
I am unsure as to what to use for the YourNameVariable. I think it needs to be an instance of Routine (or maybe the name property of Routine) but how do I create this? I am using a UITableView.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Exercise" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
[request setPredicate: [NSPredicate predicateWithFormat: #"routineExercises = %#", yourVariableNameHere]];
NSLog(#"After managedObjectContext: %#", managedObjectContext);
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[mutableFetchResults release];
[request release];
Here is my data model:
And also, should I be putting this in viewDidLoad or in the fetchResultsController's method?
You don't need a fetch if you already have the Routine objects. You just ask the Routine object for the contents of its routineExercises relationship.
You use fetches when you don't have an object to start with. If you already have an object, then you walk it's relationships to find it's associated objects. Fetching objects that are already in a relationship defeats the purpose of having relationships in the first place.
I am trying to link a value that's already in category to the wod entity. Since I do want to call a new record for each record of wod for a category. Not sure how to do this. I was thinking of using predicate but I am not exactly sure how to link it from a fetch request.
this is what my schema looks like:
Here's the code that tries to link them together:
NSManagedObjectContext *context = [self managedObjectContext];
Wod *wodInfo = [NSEntityDescription
insertNewObjectForEntityForName:#"Wods"
inManagedObjectContext:context];
[wodInfo setValue:#"Frank" forKey:#"name"];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:#"Categories"
inManagedObjectContext:context]];
[request setPredicate:[NSPredicate predicateWithFormat:#"(name == %#", #"Time"]];
// This is the part where i am unsure, since i am not exactly sure how to link them up
Category *category = reques
wodInfo.category =
NSError *error;
if (![context save:&error]) {
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
}
Any help would be greatly appreciated.
NSError *error = nil;
NSArray *categories = [context executeFetchRequest:request error:&error];
Category *category = [categories lastObject];
wodInfo.category = category;
Be careful about your call to [context save:&error]. You are passing the address of the error variable, which is not initialized in your code and will refer to a random address, which might lead to weird errors in your application. I would recommend setting it to nil before passing it to the save: method.
I want a list of unique contacts that I've stored with core data.
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Post" inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSDictionary *entityProperties = [entityDescription propertiesByName];
[request setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:#"contactID"]]];
[request setReturnsDistinctResults:YES];
NSError *error = nil;
NSMutableArray *retValue = [[[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];
The result is always the same with or without the setPropertiesToFetch, so I guess there is something wrong with it, but I can't figure out what it is.
Can someone help me?
Did you set your fetch result type to NSDictionaryResultType? The documentation says setPropertiesToFetch: only works when result type == NSDictionaryResultType
.n