Core Data: Folder like functionality - ios

Im trying to make a entity in core data that acts like a folder, like on a mac basically where you can put a object inside, and another folder. currently i have folders, and the individual bookmark (another entity) and bookmarks can be in the folder, but i cant get Folders to go inside another folder.
Here is my NSFetchedResultsController,
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Bookmark" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
[NSFetchedResultsController deleteCacheWithName:#"Folder"];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"folder == %#", self.folder];
[fetchRequest setPredicate:pred];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"title" ascending:YES];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:#"Folder"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
if the NSEntityDescription Entity was NSEntityDescription *entity = [NSEntityDescription entityForName:#"Folder" inManagedObjectContext:self.context]; it would crash with the error,
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath folder not found in entity '.
How can i fix this? i am stumped at this issue.
The Folder entity has a to-many relationship with the bookmark entity, the bookmark entity has the following attributes, title, url. Folder has the following attributes, title, displayOrder, isFolder.

NSPredicate *pred = [NSPredicate predicateWithFormat:#"folder == %#", self.folder];
This line is fine when your entity is a bookmark. If you just change the entity to Folder, and leave this predicate in place, then the fetch request will look for the key folder on a Folder entity, which doesn't exist according to your data model description above. If you want to fetch all folders, don't set a predicate.
To allow folders to contain folders, you need to define a relationship from the Folder entity to itself, something like this:
Here the Folder entity has two relationships, a to-many relationship called subfolders, and its inverse, a to-one relationship called parentFolder. The top level folder has nil for its parentFolder.
When fetching the folders contained in a folder, you either simply use the set returned by folder.subfolders, or a fetch request predicate where "parentFolder == %#",folder.

Related

NSFetchedResultsController - only show results saved in database?

I'm using Core Data and an NSManagedObject as the object model for temporarily storing the data in the app before it's saved.
Then in a tableview I display all saved instances. However, the temporary object is also appearing in the list. How can I ensure my tableview only displays results that are actually saved in the database?
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"State" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"timeStamp" ascending:NO];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
You can set
[fetchRequest setIncludesPendingChanges:NO];
From the documentation:
The default value is YES.
If the value is NO, the fetch request skips checking unsaved changes
and only returns objects that matched the predicate in the persistent
store.
NSManagedObjectContext NSFetchedResultsController will automatically save the context. So your change is already saved to database before you restore data in tableView
Core Data can manage data in-memory, which is describe as :
Core Data provides an infrastructure for change management and for
saving objects to and retrieving them from storage. It can use SQLite
as one of its persistent store types. It is not, though, in and of
itself a database. (To emphasize this point: you could for example use
just an in-memory store in your application. You could use Core Data
for change tracking and management, but never actually save any data
in a file.)
to solve your problem, you can create another NSManagedObjectContext, which use to save your temporary data. when you want to save these temp data to your database, just create new NSManagedObject and copy the data info from de temp data and save it to your permanent database context

Why do I have to comment out my NSPredicate to have NSFetchedResultsController populate an UITableView?

I have a strange bug: if I uncomment my NSPredicate, the resulting UITableView is empty.
My data Model is the following:
Category <-->> Feed <-->> Post
I am fetching the Posts. Post.feed is a Post's Feed. Feed has an rss NString property.
Here's the code:
- (NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (_fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Post"
inManagedObjectContext:_globalMOC];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"feed.rss == %#", _detailItem.rss];
[fetchRequest setPredicate:predicate];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_globalMOC
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
self.fetchedResultsController.delegate = self;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate.
// You should not use this function in a shipping application, although it may be useful
// during development. If it is not possible to recover from the error, display an alert
// panel that instructs the user to quit the application by pressing the Home button.
//
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
return _fetchedResultsController;
}
As I told before, I only see results if I uncomment the NSPredicate. I tried with LIKE, ==, =, with double and single quotes around %#...
BTW, The best would be to directly compare the Feed object...
According to Apple, the syntax might not be the issue, but then what?
The Posts are created in a separate ManagedObjectController sharing the same PersistentStoreCoordinator. I get the required Feed's objectID in order to associate the new Post with its corresponding Feed in the child MOC (otherwise I'd get an error regarding associating objects from different MOC).
I also duely merge my MOCs in the main thread whenever the child MOC notifies it of a change.
Basically: if I NSLog the Posts I have (commented-NSPredicate), I see every Post with the relevant RSS Feed URL fitting the displayed Feed (= detailItem).
Anyone can help me?
If your NSFetchedResultsController is blank then it's pretty sure that you're getting no results through the fetch request and that i'm afraid, because of inappropriate predicate statement or no matching records. i guess the problem is due to presence of wildcard(don't know much about that)
check NSPredicate Class Reference and Predicate Programming Guide to get accurate results through predicates.
Eureka!
The problem was the following: when I create my NSFetchedResultsController in my DetailView, _detailItem is nil.
So, even after when setting _detailItem, the NSPredicate still focus on comparing my feed relationship to a nil object.
I solved the problem by refreshing my NSFetchedResultsController.fetchRequest in the didSelectRowAtIndexPath in the MasterView the following way:
Feed *feed;
if (tableView == self.searchDisplayController.searchResultsTableView) {
feed = [_filteredCategoryArray objectAtIndex:indexPath.row];
} else {
feed = [[self fetchedResultsController] objectAtIndexPath:indexPath];
}
self.detailViewController.detailItem = feed;
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"feed == %#", feed];
[self.detailViewController.fetchedResultsController.fetchRequest setPredicate:predicate];
Hope this solution might help other people.
Thanks for your help, nickAtStack!

Strange bug: uncommenting an NSPredicate within an NSFetchedResultsController will empty the UITableView

I have a strange bug: if I uncomment my NSPredicate, the resulting UITableView is empty.
My data Model is the following:
Category <-->> Feed <-->> Post
I am fetching the Posts. Post.feed is a Post's Feed. Feed has an rss NString property.
Here's the code:
- (NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (_fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Post"
inManagedObjectContext:_globalMOC];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"feed.rss == %#", _detailItem.rss];
[fetchRequest setPredicate:predicate];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_globalMOC
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
self.fetchedResultsController.delegate = self;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate.
// You should not use this function in a shipping application, although it may be useful
// during development. If it is not possible to recover from the error, display an alert
// panel that instructs the user to quit the application by pressing the Home button.
//
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
return _fetchedResultsController;
}
I only see results if I uncomment the NSPredicate. I tried with LIKE, ==, =, with double and single quotes around %#...
The best would be to directly compare the Feed object...
Anyone can help me?
Solved here.
The problem was that the NSFetchedResultsController was initialized once before the _detailItem was even set.

Exception on Core Data save

This seems a fairly common workflow I have an issue with that I cannot figure why. I have a UITableView with NSFetchedResultsController supplying the rows. To add a new object, I insert into context and then present a new view controller to edit the details. The add button action is like so:
-(IBAction)addNewIssueType:(id)sender
{
IssueType *newUntitledType = [NSEntityDescription insertNewObjectForEntityForName:#"IssueType" inManagedObjectContext:self.managedObjectContext];
newUntitledType.name = NSLocalizedString(#"Untitled Task", #"Name for newly created task");
[self saveContext];
self.editingType = newUntitledType;
[self presentNameEditViewForType:newUntitledType];
}
I am crashing with exception at the saveContext method, error:
2013-05-11 16:25:35.990 App [18843:c07] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet. with userInfo (null)
2013-05-11 16:25:35.992 App [18843:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'
* First throw call stack:
Taking the notification observer suggestion as a clue, I looked at my NSFetchedResultsController, presumably the only observer of the NSManagedObjectContext in scope:
-(NSFetchedResultsController *)typesController
{
if (_typesController) {
return _typesController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"IssueType" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Filter out the required types
NSPredicate *exclude = [NSPredicate predicateWithFormat:#"NONE name in %#",excludedTypes];
[fetchRequest setPredicate:exclude];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.typesController = aFetchedResultsController;
NSError *error = nil;
if (![_typesController performFetch:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
[self failAlertWithMessage:nil forceAbort:YES];
}
return _typesController;
}
I found that, if I comment out the lines creating and assigning the predicate, it does not crash. Of course then I am showing some objects that I want hidden. I can't see anything wrong with the predicate itself, fetch does return the expected objects and excludes those in the excludedTypes Array as intended.
Save context method does not have any issues on editing or deleting existing objects, only on inserting a new object.
I am not using and threads here other than the main thread.
NONE is an alias for NOT ANY and is indeed for to-many relationships.
What you probably want is:
[NSPredicate predicateWithFormat:#"NOT name in %#", excludedTypes];

Core Data: NSPredicate containing to-many relationship yields no results when using SQLITE store type

I have been using Core Data with In-Memory store for my project until recently. Now I am trying to switch to SQLite store and I'm facing the following issue:
When Trying to fetch objects from the store using a predicate and sortDescriptor, my fetchedResultsController returns 0 objects. The exact same code works fine when the store type is in-memory.
This is how I am making the fetch:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY %K == %#", #"categories", category];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"title" ascending:YES selector:#selector(caseInsensitiveCompare:)];
self.fetchedResultsController = [self createFetchedResultsControllerWithEntityName : #"Provider" sectionName : #"group" sortDescriptor : sortDescriptor predicate : predicate cache : nil];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self.tableView reloadData];
and this is where I create the fetch request:
- (NSFetchedResultsController *)createFetchedResultsControllerWithEntityName : (NSString*) entityName sectionName : (NSString*) sectionName sortDescriptor : (NSSortDescriptor*) sortDescriptor predicate : (NSPredicate*) predicate cache : (NSString*) cacheName{
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
//set predicate
if (predicate)
[fetchRequest setPredicate:predicate];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:sectionName
cacheName:cacheName];
aFetchedResultsController.delegate = self;
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return [aFetchedResultsController autorelease];
}
Again, this very same code fetches many objects when using in-memory store, but an empty fetchedObjects array when using SQLite.
I've read Apple's documentation with regard to the special considerations to make when using predicates and sortDescritors with SQLite store. I am using a single (many-)to-many relationship (categories) in the predicate, so this should be fine?
There is no error returned from the fetch. Simply no objects (fetchedResultsController.fetchedObjects is empty).
Any guidance would be greatly appreciated.
Thanks
I've figured out this issue eventually.
My query returned no results there were indeed no results. Tracing this back to where I was creating the objects and populating the realtionships with values I noticed that I was doing it wrong. I tried to do:
[provider.categories addObject : newCategory];
Which did not really add newCategory to the categories relationship.
After having realized that Core Data generates the accessors for to-many relationships automatically, I used the following to add newCategory:
[provider addCategoriesObject : newCategory];
This properly added newCategory to the relationship.
In addition to that I declared addCategoriesObject in Provider.h to suppress the compiler warning. This resolved the issue for me.
The only remaining caveat is that the compiler does not find the implementation of addCategoriesObject: and warns about "incomplete implementation". I haven't figured out a way to suppress this warning too.
I know SQLite stores have problems with "ANY" fetches in some cases (I don't remember the exact ones). Maybe try modifying your search predicate to see if you get different results, e.g.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"categories LIKE %#", category];

Resources