Function in NSManagedObjectClass is failing - iphone

I have a NSManagedObjectClass Favorites. In this class I have a method getFavoritesByArtistId You can see it over here.
-(Favorites *)getFavoriteById:(int)art_id{
Favorites *favorite;
NSLog(#"artist is is %d",art_id);
RKManagedObjectStore *store = [[GenkonStageDataModel sharedDataModel] objectStore];
NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Favorites"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"fav_art_id == %d",art_id];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:#"fav_id" ascending:YES];
fetchRequest.sortDescriptors = #[descriptor];
NSArray *matches = [context executeFetchRequest:fetchRequest error:nil];
if (matches.count > 0){
NSLog(#"till here");
favorite = [matches objectAtIndex:0];
}else {
NSLog(#"till here 2");
return NULL;
}
return favorite;
}
In my view controller I am calling it like this.
RKManagedObjectStore *store = [[GenkonStageDataModel sharedDataModel] objectStore];
NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Favorites" inManagedObjectContext:context];
Favorites* favClass = [[Favorites alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:context];
Favorites *favorite = [favClass getFavoriteById:[artist.art_id intValue]];
Next I'm going to check
if(![favorite isKindOfClass:[NSNull class]]){
//Artist is already favorized !
}else{
//Artist is not favorized !
}
But my app always crashes with this CoreData _PFFastEntityRangesByType + 12, stop reason = EXC_BAD_ACCESS (code=1, address=0x38)
Any help?

Related

Database update using Core Data

I am trying to update my local database when app gets response from the web server. When app gets the update from web server, I fetch the data from the local database by matching the id with the response and get one row and perform update code but local database does not get updated and also does not give an error.
What should be the solution???
-(void)checkID:(NSMutableDictionary *)dict
{
NSDictionary *dictEvent = [dict objectForKey:#"Event"];
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *selectedManagedObject = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Events" inManagedObjectContext:context];
NSSortDescriptor *sortDescObj = [[NSSortDescriptor alloc] initWithKey:#"event_id" ascending:YES];
NSError *error = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"user_id=%# and event_id=%#",[NSNumber numberWithInt:[[dictEvent valueForKey:#"user_id"] intValue]],[NSNumber numberWithInt:[[dictEvent valueForKey:#"id"] intValue]]]];
NSLog(#"Predicate = %#",predicate);
NSArray *arrSortDescriptors = [NSArray arrayWithObject:sortDescObj];
[fetchRequest setSortDescriptors:arrSortDescriptors];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPredicate:predicate];
NSArray *arrResult = [context executeFetchRequest:fetchRequest error:&error];
if ([arrResult count]>0)
{
NSArray *arrKey = [dictEvent allKeys];
NSArray *arrValue = [dictEvent allValues];
NSLog(#"ArrKey : %#\nArrValue : %#",arrKey,arrValue);
selectedManagedObject = [arrResult objectAtIndex:0];
for(int i = 0; i < [arrKey count] ; i++)
{
NSLog(#"selectedMng :- %#",selectedManagedObject);
NSLog(#"KEY: %#\t: %#",[arrKey objectAtIndex:i],[arrValue objectAtIndex:i]);
if ([[arrKey objectAtIndex:i]isEqualToString:#"id"])
{
[selectedManagedObject setValue:[arrValue objectAtIndex:i] forKey:#"event_id"];
}
else if([[arrKey objectAtIndex:i]isEqualToString:#"invited_status"])
{
[selectedManagedObject setValue:[arrValue objectAtIndex:i] forKey:#"invite_status"];
}
else
{
[selectedManagedObject setValue:[arrValue objectAtIndex:i] forKey:[arrKey objectAtIndex:i]];
}
}
if (! [selectedManagedObject.managedObjectContext save:&error])
{
NSLog(#"updateEntityIntoDataBaseNamed - Error :: %#", [error localizedDescription]);
}
// }
}
}
Besides modifying your predicate as suggested by #Martin
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"user_id=%# && event_id=%#",
[NSNumber numberWithInt:[[dictEvent valueForKey:#"user_id"] intValue]],
[NSNumber numberWithInt:[[dictEvent valueForKey:#"id"] intValue]]
];
note that in two cases, you are updating your object using non matching keys: this happens for id and event_id, and for invited_status and invite_status.
You cannot use stringWithFormat within predicateWithFormat. Your predicate should probably look like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"user_id=%# and event_id=%#",
[NSNumber numberWithInt:[[dictEvent valueForKey:#"user_id"] intValue]],
[NSNumber numberWithInt:[[dictEvent valueForKey:#"id"] intValue]]
];

UITableView cellForRowAtIndexPath causing slow scrolling

My table is scrolling incredibly slow, and I think it's caused by the Core Data methods within my cellForRowAtIndexPath. Below is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSManagedObject *info = [buildingArray objectAtIndex: [indexPath row]];
// all rooms have been scanned for building
if([self allRoomsScanned: [[info valueForKey:#"buildingid"] intValue]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell.textLabel setTextColor: [UIColor lightGrayColor]];
}
else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.textLabel setTextColor: [UIColor blackColor]];
}
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[cell.textLabel setText:[info valueForKey:#"buildingname"]];
return cell;
}
And here is the allRoomsScanned method and allDevicesScanned method:
- (BOOL) allRoomsScanned: (int) buildingID {
NSMutableArray *scannedRoomArray = [[NSMutableArray alloc] init];
// Get all user_device
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"user_device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSNumber *deviceid = [NSNumber numberWithInt: 0];
NSNumber *roomid = [NSNumber numberWithInt: 0];
//int lastRoomID = 0;
for (NSManagedObject *info in fetchedObjects) {
// Get all device
deviceid = [info valueForKey:#"deviceid"];
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(deviceid = %d)", [deviceid intValue]];
[fetchRequest setPredicate:predicate];
NSArray *fetchedDevices = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *infod in fetchedDevices) {
// Get all room
roomid = [infod valueForKey:#"roomid"];
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"room" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(roomid = %d) AND (buildingid = %d)", [roomid intValue], buildingID];
[fetchRequest setPredicate:predicate];
NSMutableArray *fetchedRoom = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];
// add room to array if room belongs to selected building and room not already added
if([fetchedRoom count] > 0) { //&& lastRoomID != [roomid intValue]) {
for (NSManagedObject *info in fetchedRoom) {
NSLog(#"room id: %#", [info valueForKey:#"roomid"]);
// add room ids to array if not already there
if (![scannedRoomArray containsObject:[info valueForKey:#"roomid"]] && [self allDevicesScanned:[[info valueForKey:#"roomid"] intValue]])
[scannedRoomArray addObject: [info valueForKey:#"roomid"]];
}
//lastRoomID = [roomid intValue];
}
}
}
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"room" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(buildingid = %d)", buildingID];
[fetchRequest setPredicate:predicate];
NSArray *fetchedRoomTotal = [context executeFetchRequest:fetchRequest error:&error];
//NSLog(#"Total Rooms for Building: %d", [fetchedRoomTotal count]);
//NSLog(#"Scanned Rooms for Building: %d", [scannedRoomArray count]);
//NSLog(#"Scanned rooms: %#", scannedRoomArray);
if([fetchedRoomTotal count] == [scannedRoomArray count] && [fetchedRoomTotal count] > 0) {
return YES;
}
else {
return NO;
}
}
- (BOOL) allDevicesScanned: (int) roomID {
NSMutableArray *scannedDeviceArray = [[NSMutableArray alloc] init];
// Get all user_device
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"user_device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSNumber *deviceid = [NSNumber numberWithInt: 0];
//NSNumber *roomid = [NSNumber numberWithInt: 0];
for (NSManagedObject *info in fetchedObjects) {
// Get all device
deviceid = [info valueForKey:#"deviceid"];
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(deviceid = %d) AND (roomid = %d)", [deviceid intValue], roomID];
[fetchRequest setPredicate:predicate];
NSArray *fetchedDevices = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *infod in fetchedDevices) {
// add device to array
if([fetchedDevices count] > 0) {
NSLog(#"room id: %d", roomID);
// add device ids to array if not already there
if (![scannedDeviceArray containsObject:deviceid])
[scannedDeviceArray addObject: deviceid];
}
}
}
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(roomid = %d)", roomID];
[fetchRequest setPredicate:predicate];
NSArray *fetchedDeviceTotal = [context executeFetchRequest:fetchRequest error:&error];
//NSLog(#"Total Devices for Room: %d", [fetchedDeviceTotal count]);
//NSLog(#"Scanned Devices for Room: %d", [scannedDeviceArray count]);
//NSLog(#"Scanned Devices: %#", scannedDeviceArray);
if([fetchedDeviceTotal count] == [scannedDeviceArray count] && [fetchedDeviceTotal count] > 0) {
return YES;
}
else {
return NO;
}
}
Any idea on how to get rid of the latency when scrolling? I'm assuming I may be doing something inefficiently with either my core data calls or the way I'm calling the method in cellForRowAtIndexPath.
Thank for any help. It is greatly appreciated.
You really should not make Fetch Requests and processing like that on the main thread while scrolling a table view.
As rokjarc said, you should definitely save the result of your (quite heavy) allRoomsScanned method. I'd suggest adding a new style, i.e. with an activity indicator, that the cell gets when you don't have a result for that input yet. As soon as the load is complete you refresh the table view cell.
Attention: You can't use your default NSManagedObjectContext in allRoomsScanned and allDevicesScanned.
You need to initialize a new context in the background thread. Either initialize a new context at the beginning of the block and pass it as a method parameter or create a new one right in the methods.
NSManagedObject *info = [buildingArray objectAtIndex: [indexPath row]];
NSNumber *cachedResult = [self.scanResults objectForKey:info.objectID];
if (cachedResult == nil) {
// style loading state
int scanInfo = [[info valueForKey:#"buildingid"] intValue];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL result = [self allRoomsScanned: scanInfo];
[self.scanResults setObject:[NSNumber numberWithBool:result] forKey:info.objectID];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]
});
} else if (cachedResult.boolValue == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell.textLabel setTextColor: [UIColor lightGrayColor]];
} else if (cachedResult.boolValue == NO) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.textLabel setTextColor: [UIColor blackColor]];
}
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[cell.textLabel setText:[info valueForKey:#"buildingname"]];
return cell;

Cannot executeFetchRequest after database is created

I get this error when trying to add a record to my database:
2012-02-12 20:15:18.187 Flavma[3197:707] CoreData: error: Serious application error.
An exception was caught from the delegate of NSFetchedResultsController during a call to -
controllerDidChangeContent:. *** -[__NSArrayI objectAtIndex:]:
index 1 beyond bounds [0 .. 0] with userInfo (null)
I tried adding every way I can, but what I'm currently using is this category:
#import "Patient+Create.h"
#implementation Patient (Create)
+ (Patient *)patientWithLastName:(NSString *)lastName inManagedObjectContext:(NSManagedObjectContext *)context
{
Patient *patient = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Patient"];
request.predicate = [NSPredicate predicateWithFormat:#"lastName = %#", lastName];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"lastName" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *patients = [context executeFetchRequest:request error:&error];
if (!patients || ([patients count] > 1)) {
//handle error
} else if (![patients count]) {
//create a new one
patient = [NSEntityDescription insertNewObjectForEntityForName:#"Patient" inManagedObjectContext:context];
patient.lastName = lastName;
} else {
patient = [patients lastObject];
}
return patient;
}
#end
I am able to add data to my database when it's first created (if I delete the app from my device), like so:
- (void) fetchPatientDataIntoDocument:(UIManagedDocument *)document
{
dispatch_queue_t fetchQ = dispatch_queue_create("Patient fetcher", NULL);
dispatch_async(fetchQ, ^{
[document.managedObjectContext performBlock:^{
[Patient patientWithLastName:#"Johnson" inManagedObjectContext:self.patientDatabase.managedObjectContext];
}];
});
dispatch_release(fetchQ);
}
But after that, I keep getting the same error. Any ideas?
#import "Patient+Create.h"
#implementation Patient (Create)
+ (Patient *)patientWithLastName:(NSString *)lastName inManagedObjectContext:(NSManagedObjectContext *)context
{
Patient *patient = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Patient"];
request.predicate = [NSPredicate predicateWithFormat:#"lastName = %#", lastName];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"lastName" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *patients = [context executeFetchRequest:request error:&error];
if (!patients || [patients count]<=0) {
//create a new one
patient = [NSEntityDescription insertNewObjectForEntityForName:#"Patient" inManagedObjectContext:context];
patient.lastName = lastName;
[context performSelectorOnMainThread:#selector(save:) withObject:nil waitUntilDone:YES];
} else {
patient = [patients lastObject];
}
return patient;
}
#end
Just save the newly created object on main thread.
Reason for this, you are creating the object on a secondary thread(GCD), those changes will not effect until you save the context on main thread
[context performSelectorOnMainThread:#selector(save:) withObject:nil waitUntilDone:YES];

problem with NSSortDescriptor .i am sorting the in ascending order(alphabetical order)

I am using NSSortDescriptor to sort the custom objects with key bandName, but I am not getting the output in alphabetical order. How do I fix that?
-(void)getdetails
{
NSLog(#"in getdetail method");
SongRequestAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context1 = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"AddToFav" inManagedObjectContext:context1];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error;
NSMutableArray *array_new=[[NSMutableArray alloc]init];
[array_new addObjectsFromArray:[context1 executeFetchRequest:request error:&error]];
//[self.fetchedObjects addObjectsFromArray:[context1 executeFetchRequest:request error:&error]];
[request release];
**NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"bandName" ascending:YES] autorelease];
NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
//self.fetchedObjects=
[array_new sortedArrayUsingDescriptors:sortDescriptors];
[self.fetchedObjects addObjectsFromArray:array_new];
[array_new release];**
for (int i=0; i<[self.fetchedObjects count];i++)
{
NSManagedObject
*addfav=[self.fetchedObjects objectAtIndex:i];
addFavObject *addfavobject=[[addFavObject alloc]init];
addfavobject.bandImagePath=[addfav valueForKey:#"bandImagePath"];
addfavobject.bandId=[addfav valueForKey:#"bandId"];
addfavobject.bandName=[addfav valueForKey:#"bandName"];
addfavobject.songId=[addfav valueForKey:#"songId"];
addfavobject.songListId=[addfav valueForKey:#"songListId"];
addfavobject.songName=[addfav valueForKey:#"songName"];
addfavobject.bandRealName=[addfav valueForKey:#"bandRealName"];
addfavobject.biography=[addfav valueForKey:#"biography"];
NSLog(#"addto fav object biography is %#",addfavobject.biography);
[self.addTofavObjectArray addObject:addfavobject];
[addfavobject release];
}
//NSArray *reverse = [[self.addTofavObjectArray reverseObjectEnumerator] allObjects];
// [self.addTofavObjectArray removeAllObjects];
// [self.addTofavObjectArray addObjectsFromArray:reverse];
NSLog(#"self.addTofavObjectArray is %#",self.addTofavObjectArray);
NSLog(#"FETCHED OBJECTS count is %d",[self.fetchedObjects count]);
}
sortedArrayUsingDescriptors: doesn't sort in place the array it was sent to. It returns a new array with the objects sorted. It should work with:
NSArray *sortedArray = [array_new sortedArrayUsingDescriptors:sortDescriptors];
[self.fetchedObjects addObjectsFromArray:sortedArray];
You don't need to do the sort separately like that though. You can give the sort descriptors to your fetch request and the array returned from executing it will be sorted. I'd highly recommend doing it this way instead of how you're doing it. So you can replace everything before the for loop with:
SongRequestAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context1 = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"AddToFav" inManagedObjectContext:context1];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"bandName" ascending:YES] autorelease];
NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
[sortDescriptor release];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setSortDescriptors:sortDescriptors];
NSError *error;
NSMutableArray *array_new = [context1 executeFetchRequest:request error:&error];
if (!array_new)
{
NSLog(#"error: %#", error);
}
[self.fetchedObjects addObjectsFromArray:array_new];
[request release];
I also added in the check for array_new to print the error. You might want to add additional error handling there too.

NSFetchedResultsController refresh refetch?

I want to refetch data from my NSFetchedResultsController using a different predicate which is set using a boolean value. How do I refresh NSFetchedResultsController to fetch a new set of data?
- (void)refreshFetchedResultsController {
NSLog(#"refreshFetchedResultsController");
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error loading data",
#"Error loading data")
message:[NSString stringWithFormat:
NSLocalizedString(#"Error was: %#, quitting.", #"Error was: %#, quitting."),
[error localizedDescription]]
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", #"Cancel")
otherButtonTitles:nil];
[alert show];
}
}
which calls
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
NSLog(#"i was executed.");
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
TapAppDelegate *appDelegate = (TapAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Favorite" inManagedObjectContext:managedObjectContext];
NSString *sectionKey = #"favname";
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"favname" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
if(showAll == NO){
if(isXSelected == NO){
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isFirst == TRUE"];
[fetchRequest setPredicate:predicate];
}
if(isXSelected == YES){
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isFirst == FALSE"];
[fetchRequest setPredicate:predicate];
}
}
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:sectionKey
cacheName:nil];
[sortDescriptor release];
[sortDescriptors release];
frc.delegate = self;
_fetchedResultsController = frc;
[fetchRequest release];
return _fetchedResultsController;
}
Here's how we do it in an application:
// Assuming these exist
NSPredicate * predicate;
NSString * cacheName;
[[fetchedResultsController fetchRequest] setPredicate:predicate];
[NSFetchedResultsController deleteCacheWithName:cacheName];
NSError * error = nil;
[fetchedResultsController performFetch:&error];
if (error) {
// report error
}
Don't forget to set fetchedResultsController = nil before performFetch. Otherwise it will use the old one.
I had similar problem, I couldn't figure out why my collection view doesn't refresh its cells.
Adding the method below fixed my problems.
-(void)didUpdateObjectAtIndexPath:(NSIndexPath *)indexPath{
UICollectionView *strongCollectionView = self.collectionView;
[strongCollectionView reloadItemsAtIndexPaths:#[indexPath]];
}