1

I'm new to Core Data and I need some help on my project.

I'm developing iPhone app that get JSON (Restaurants info) from server and shows the location on the map and table view, and stores favorite restaurants by pressing "add favorite" button.

For now, I'm just using NSDictionary and its functions to display data on the table and annotations and having favoriteRestaurant entity to store favorite restaurant data.

However, I would like to convert the NSDictionary object into Core Data object (Restaurant), and add "BOOL isFavorite" attribute to it and then delete favoriteRestaurant entity. Make function that saves the restaurant object that passed and changes its "isFavorite" state, which is triggered by "add Favorite" button.

The favorite table shows only the restaurants that has been saved and isFavorite = YES.

I would like to know if this is right approach to accomplish what I want. Thank you in advance!


Hi, thank you for fast responses. I forgot to say that I also want to implement MKAnnotation to that class so each annotation pin on the map belongs to unique restaurant object. If I want to do this, should I have another favorite class or Core Data entity, or just save it in the Restaurant table and make isFavorite = YES? Thank you, again!

kazuochi
  • 230
  • 1
  • 3
  • 11
  • It would work, but I wouldn't user Core Data for such as simple case, especially if you're new to it. You can save your NSDictionaries to the disk, and add a key "isFavorite" in your different restaurants, and update it. You can use NSPredicates with NSDictionaries like you would do with CoreData – Julien Aug 04 '11 at 20:58
  • Thank you for the answer. But I forgot to say that I want to implement MKAnnotation to each restaurant. I think CoreData is only the way to do so. – kazuochi Aug 05 '11 at 00:33

2 Answers2

0

If you're only concerned about saving the favorites then id go with plist serialization. I do the same thing in multiple apps. Normally I create a Helper class like below.


#import "Favorites.h"
#import "NSArray+Locations.h"

@implementation Favorites

+(void)addFavorite:(Location *)location{
    NSMutableArray *remove = [NSMutableArray array];
    [location.data enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSLog(@"%@,%@, %@",key,obj,[obj class]);
        if([obj isKindOfClass:[NSNull class]])
            [remove addObject:key];
    }];
    [location.data removeObjectsForKeys:remove];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * favorites = [NSMutableDictionary dictionaryWithDictionary:[defaults objectForKey:FAVORITES]];
    if(!favorites)
        favorites = [NSMutableDictionary dictionaryWithCapacity:1];
    [favorites setObject:location.data forKey:[location getID]];
    [defaults setObject:favorites forKey:FAVORITES];
    [defaults synchronize];
}

+(void)removeFavorite:(Location *)location{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * favorites = [NSMutableDictionary dictionaryWithDictionary:[defaults objectForKey:FAVORITES]];
    [favorites removeObjectForKey:[location getID]];
    [defaults setObject:favorites forKey:FAVORITES];
    [defaults synchronize];
}

+(NSArray *)getFavorites{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * favorites = [defaults objectForKey:FAVORITES];
    return [NSArray arrayWithLocations:[favorites allValues]];
}

+(BOOL)isFavorited:(Location *)location{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * favorites = [defaults objectForKey:FAVORITES];
    for(NSString *key in [favorites allKeys])
        if([[location getID] isEqualToString:key])
            return YES;
    return NO;
}

@end


In this case the Location object is just a wrapper around a dictionary to make accessing the fields simpler.

If you want to save all of your data and not just favorites then I would go with core data otherwise a very large plist could give you memory headaches.

Dave.B
  • 6,632
  • 1
  • 19
  • 20
  • Thank you for the answer to my question. Do you think this is the better way even I want to use Core Data for restaurants? – kazuochi Aug 05 '11 at 00:35
  • This way is faster than using core data imo but core data is more scalable. – Dave.B Apr 30 '12 at 21:46
0

I like your approach. In my opinion Core Data is the way to go.

If you get lots of JSON records (say, dozens or hundreds of restaurants) with maybe even more data fields in the future, you could run into memory problems when using an array of NSDictionarys. Remember, a serialized plist can only be retrieved entirely, so if it gets large you will have to keep all the data in memory.

Also, you will very like have a much better performance from the start.

Your BOOL attribute (in Core Data that would be a NSNumber) should work fine for your purpose.

Mundi
  • 79,884
  • 17
  • 117
  • 140