Deleting objects in core data depending on JSON - ios

I have a NSManagedObjectModel with an entity, "Project". I get all the projects to show them in a UITableView with NSFetchedResult controller. Now if the JSON has new Projects I insert them and if the JSON has updated items I update the items in the Core Data context.
So, my problem is when I get a JSON with less items than the context. I have thought about two ways to delete items in my context. One way is Delete all the context and save it again with the new items. The other way is create an array with all the items in the context and check it with the items in the JSON by id and if there is not one item, then remove it.
I have the idea, but I don't know which is the best way. I have thought also in a backgroundContext.
I use this method right now with out deleting methods:
#pragma mark - Project List service
- (void)getProjectListWithCpompletionBlock:(CompletionBlock)completionBlock{
NSMutableURLRequest *request = [self requestWithMethod:#"GET" path:kAPIProjectList parameters:nil];
[request setTimeoutInterval:kTimeOutRequest];
AFJSONRequestOperation *requestOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *projects = [JSON valueForKey:kTagProjects];
for (NSDictionary *projectDic in projects) {
Project *pro = [Project getProjectWithId: [projectDic objectForKey:kTagProjectId] ];
if (pro) {
[Project updateProjectWithDictionary:projectDic];
NSLog(#"update %# ",[projectDic objectForKey:kTagProjectId]);
} else {
[Project createProjectWithDictionary: projectDic];
NSLog(#"create %# ",[projectDic objectForKey:kTagProjectId]);
}
}
[ypCoreDataManager saveContext];
if (completionBlock) {
completionBlock(NO, nil);
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *aError, id JSON) {
NSLog(#"%# Failure JSNON Error%#", NSStringFromSelector(_cmd), aError);
if (completionBlock) {
completionBlock(YES, aError);
}
}];
[self enqueueHTTPRequestOperation:requestOperation];
}
Project+Helper is my project category and here it is the code.
+ (Project *)createProjectWithDictionary:(NSDictionary *)dic {
Project *project = nil;
project = [NSEntityDescription insertNewObjectForEntityForName:#"Project" inManagedObjectContext:mainContext];
project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
project.title = [[dic valueForKey:kTagProjectTitle]description];
project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
NSMutableArray *tags = [[NSMutableArray alloc] init];
tags = [dic objectForKey:kTagProjectsTags];
NSMutableSet *tagSet = [[NSMutableSet alloc]init];
for (NSDictionary * tagDic in tags){
NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
Tag *tag = [Tag insertTagName:tagName inManagedObjectContext:mainContext];
[tagSet addObject:tag];
}
[project addTags:tagSet];
return project;
}
// Return project by id
+ (Project *)getProjectWithId:(NSString *) projectId {
Project *project = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Project"];
request.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"projectId" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:#"projectId = %#", [projectId description]];
// Execute the fetch
NSError *error = nil;
NSArray *matches = [mainContext executeFetchRequest:request error:&error];
if (!matches || ([matches count] > 1)) { // nil means fetch failed; more than one impossible (unique!)
// handle error
} else { // found the Project, just return it from the list of matches (which there will only be one of)
project = [matches lastObject];
}
return project;
}
// Update project
+ (Project *)updateProjectWithDictionary:(NSDictionary *)dic {
Project *project = nil;
// Build a fetch request to see if we can find this Project in the database.
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Project"];
request.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"title" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:#"projectId = %#", [dic[kTagProjectId]description]];
// Execute the fetch
NSError *error = nil;
NSArray *matches = [mainContext executeFetchRequest:request error:&error];
// Check what happened in the fetch
if (!matches || ([matches count] > 1)) { // nil means fetch failed; more than one impossible (unique!)
// handle error
} else {
project = [matches lastObject];
project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
project.title = [[dic valueForKey:kTagProjectTitle]description];
project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
NSMutableArray *tags = [[NSMutableArray alloc] init];
tags = [dic objectForKey:kTagProjectsTags];
NSMutableSet *tagSet = [[NSMutableSet alloc]init];
for (NSDictionary * tagDic in tags){
NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
Tag *tag = [Tag insertTagName:tagName inManagedObjectContext:mainContext];
[tagSet addObject:tag];
}
[project addTags:tagSet];
}
return project;
}

You have to add this method in your Project's category and in your code after you add the new item call this method where you pass you array objects living in the Core Data and it remove all the objects more that you haven't in the array
+(void)removeExpiredProjectBy:(NSMutableArray *)ProjectLiving inContext:(NSManagedObjectContext *)context{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Project"];
if (projectLiving.count) {
request.predicate = [NSPredicate predicateWithFormat:#"NOT (projectId IN %#)", [projectLiving copy]];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (matches.count != 0) {
for (Project *pro in matches) {
[context deleteObject:pro];
}
}
}
}

As you process the JSON you can build a list of all of the ids that have been added / updated. Then, after that is complete you can create a fetch request with a predicate which finds all items where NOT (id IN %#) and supply the list of ids. This will return you only the items that need to be deleted.
Or, from an efficient API point of view, the server should give you a deletion list because it doesn't need to confirm items that haven't changed if you send it a 'last request date'...

Related

Update Core Data with JSON Key Value ( id )

I am having a problem with updating the core data.
i am downloading the data in the background thread comparing with the identifier in core data and count but am having trouble to update now i want to check the identifier present in Core data with JSON Response and if id is present in the JSON Response leave it and if not present in JSON Response (That means That record has been removed in Server side)
Here in this code am checking id is present in core data or not and now i want to check the id is present in json or not to update the records
Any help will be great appreciate thanks in advance
please check the code how am storing the data in to core data
for (int i = 0; i < [arrayData count]; i++) {
NSNumber * idNum = [arrayData objectAtIndex:i][#"id"];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Deal"];
[request setPredicate:[NSPredicate predicateWithFormat:#"identifier == %#",idNum]];
[request setFetchLimit:1];
NSUInteger count = [_managedObjectContext countForFetchRequest:request error:&error];
if (count == NSNotFound) {
NSLog(#"ERROR");
}else if (count == 0) {
Deal * dealsEntity = [NSEntityDescription insertNewObjectForEntityForName:#"Deal" inManagedObjectContext:_managedObjectContext];
NSString * name = [arrayData objectAtIndex:i][#"name"];
dealsEntity.nameAttribute = name;
dealsEntity.identifier = idNum;
[appDelegate saveContext];
}
[self performSelectorOnMainThread:#selector(updateData:) withObject:_myArray waitUntilDone:YES];
}
- (void) updateData:(NSArray *)yourData {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Deal" inManagedObjectContext:_managedObjectContext];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setEntity:entity];
NSError *error;
yourData = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
self.myArray = yourData;
[listTableView reloadData];
}
I have tried this with updating or deleting the records from coredata
for (int d = 0; d < [_myArray count]; d++) {
Deal * deal = (Deal*)_myArray[d];
NSNumber * identifier = [deal identifier];
if ([identifier isEqualToNumber:[[arrayData objectAtIndex:d] valueForKey:#"id"]] ) {
NSLog(#"equal %d",d);
} else {
NSLog(#"Kill it ");
}
}
but here the problem is Coredata has 115 records when checking with json but json will have only 114 records and it returns crash
As you have the identifier into your database you just need to change your code little bit
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:#"Deal" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:#"identifier == %#",idNum]];
[request setFetchLimit:1];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
// check the count
if([results count] == 1) {
// Update your coredata object
} else {
// Create new object
}

Search of UITableView

I have a UITableView populated from a JSON data feed using an NSDictionary and NSArray and I am trying to search the UITableView data using this code, but it is not working some other related info based on how I set it up is in this question Data from JSON Parsing straight to UITableView
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredCandyArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name contains[c] %#",searchText];
NSArray *tempArray = [airportsArray filteredArrayUsingPredicate:predicate];
NSLog(#" text %#", searchText);
filteredCandyArray = [NSMutableArray arrayWithArray:tempArray];
NSLog(#"NSLog %#", scope);
}
This is how the data is parsed
NSString* path = #"https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=532ca6af&appKey=50d6d3351e5953152b2688f10d10d276";
NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:#"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];
if(nil != error)
{
NSLog(#"Error: %#", error);
}
else
{
NSMutableDictionary* json = nil;
if(nil != _connectionData)
{
json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
}
if (error || !json)
{
NSLog(#"Could not parse loaded json with error:%#", error);
}
else
{
routeRes = [json objectForKey:#"airports"];
airportsArray = [json objectForKey:#"airports"];
}
_connectionData = nil;
NSLog(#"connection done");
}
NSLog(#" end array %#", candyArray);
// Initialize the filteredCandyArray with a capacity equal to the candyArray's capacity
filteredCandyArray = [NSMutableArray arrayWithCapacity:[airportsArray count]];
// Reload the table
[NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
[[self tableView] reloadData];
You can manually build the array via the build in loop methods,
NSMutableArray *filteredResults = [NSMutableArray new];
[airportsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj.name rangeOfString: searchText].location != NSNotFound) {
[filteredResults addObject:obj];
}
}];
self.filteredCandyArray = [NSArray arrayWithArray:filteredResults];
You will need to replace the id obj with what ever object model you are using. For example if you had a plane model you might do Plane *plane in replace of id obj.
I might add how to use it via predicates later, but this should do what you request. You may view the enumerateObjectsUsingBlock: and arrayWithArray: methods in the NSArray documentation. rangeOfString: can be viewed in the NSString documentation.
-NSArray Documentation
-NSString Documentation

Compare the contents of two arrays and finding the differences

I have two arrays, one is an array from a fetch request of core-data (array1), the other is from data that is being pulled from the web(array2). What I want to do is compare array1 to array2 and any items that are in array2 that are not in array1 need to be added to core-data.
The data I'm pulling from has id's associated with each person. When I create a new "person" entity I save this id with it as well. I'm not sure how to compare the arrays using the Person's id, or even how to access that within an array.
Here is the fetch request:
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSError *error = nil;
[fetch setEntity:[NSEntityDescription entityForName:#"Person" inManagedObjectContext:self.managedObjectContext]];
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:#"Pid" ascending:YES];
request.sortDescriptors = #[sorter];
NSArray *items = [self.managedObjectContext executeFetchRequest:request error:&error];
I have the fetch sorted in the same way array2 with the new data is sorted. I'm just not sure how to compare the two and then add the new items into core-data. Please help?
Update:
NSDictionary *qDict = [JSON objectForKey:#"person"];
NSArray *qArry = [JSON objectForKey:#"person"];
//Used to print the id's
_testArray = [NSMutableArray arrayWithArray:[qDict valueForKey:#"id"]];
for (NSNumber *numb in _testArray) {
NSLog(#"id = %#", numb);
}
NSError *error = nil;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Person"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Pid IN %#", [qDict valueForKey:#"Pid"]];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"Pid" ascending:YES]]];
NSArray *items = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(#"%d how many items from the fetch", items.count);
for (NSDictionary *qdt in qArry) {
NSUInteger currentIndex = 0;
Person *q = nil;
if ([items count] > currentIndex) {
q = [items objectAtIndex:currentIndex];
}
if ([q.Pid integerValue] == [[qdt objectForKey:#"id"] integerValue]) {
// Either update the object or just move on
}
else {
// Add the object to core data
q = [NSEntityDescription insertNewObjectForEntityForName:#"Person" inManagedObjectContext:self.managedObjectContext];
q.url = [qdt valueForKey:#"url"];
q.Pid = [qdt objectForKey:#"id"];
q.text = [qdt valueForKey:#"personText"];
NSError *error = nil;
[_managedObjectContext save:&error];
}
currentIndex++;
}
//[_managedObjectContext save:&error];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Error retrieving data" message:#"Please try again" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[av show];
}];
First you can fetch the list of objects stored in your core data based on person's IDs that you retrieve from the web (idArray) and sort them based on the ID:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Person"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"id IN %#", idArray];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"id" ascending:YES]]];
This will return you the list of objects that the id can also be found in the objects you retrieve from the web. Let us call this storedRecords array. Then you can do the following:
Set a counter,
Iterate over the downloadedArray (this is the array containing the objects retrieved from the web and it must be sorted by id too),
Use objectAtIndex in the storedRecords array,
Check the id of the storedManagedObject if it matches the id of the record object
if it does not match, it is a new object that you can save into core data. Otherwise, it is an existing object.
Increase the counter.
Here is an example:
int currentIndex = 0;
for (NSDictionary *record in downloadedArray) {
NSManagedObject *storedManagedObject = nil;
if ([storedRecords count] > currentIndex) {
storedManagedObject = [storedRecords objectAtIndex:currentIndex];
}
if ([[storedManagedObject valueForKey:#"id"] integerValue] == [[record valueForKey:#"id"] integerValue]) {
//this will be existing object in core data
//you can update the object if you want
} else {
//this will be new object that you can store into core data
}
currentIndex++;
}
This is similar to what is mentioned here:
http://www.raywenderlich.com/15916/how-to-synchronize-core-data-with-a-web-service-part-1
maybe you can do something like this:
NSMutableArray *webData = [NSMutableArray arrayWithObjects:#"object 1",#"object 2",#"object 3",#"object 4", nil];
NSMutableArray *coreData = [NSMutableArray arrayWithObjects:#"object 2",#"object 4", nil];
NSMutableArray *newData = [NSMutableArray arrayWithArray:webData];
[newData removeObjectsInArray:coreData];
//output "object 1", "object 3"
or if you have a custom NSObject inside the array and compare it with its primary key maybe you can do something like:
NSMutableArray *webData = [NSMutableArray arrayWithObjects:test1,test2,test3,test4, nil];
NSMutableArray *coreData = [NSMutableArray arrayWithObjects:test2,test3, nil];
__block NSMutableArray *newData = [NSMutableArray new];
[webData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
__block testObject *object = (testObject *)obj;
__block BOOL isExisting = NO;
[coreData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
testObject *innerObject = (testObject *)obj;
if(object._id == innerObject._id)
isExisting = YES;
}];
if(!isExisting)
[newData addObject:object];
}];
//output will be object 1 and object 4
Try this
NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:#"a",#"a",#"a",#"d",#"b",#"b",#"c",#"a",#"c",nil];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"%#",[array1 objectAtIndex:0]], nil];;
for(int i = 0;i<[array1 count];i++)
{
if ([array2 containsObject:[array1 objectAtIndex:i]]) {
NSLog(#"do nothing");
}
else{
[array2 addObject:[array1 objectAtIndex:i]];
NSLog(#"array2 is %#",array2);
}
NSLog(#"finally array2 is %#",array2);
}
array2 will be as { a,d,b,c }

JSON parse to Core Data works, but I get null objects

The problem I'm encountering when trying to save JSON to Core Data is that I get null objects on my list view instead of having them populated with data. This is my JSON format:
[{"nid":"23","name":"JN01"},{"nid":"24","name":"JN02"}]
This is my code
jsonToArray.h
#define JSON_NAME #"name"
#define JSON_NID #"nid"
#interface JSONToArray : NSObject
#property (nonatomic, copy) NSString *nid;
#property (nonatomic, copy) NSString *name;
+ (void)showNetworkError:(NSString *)theMessage;
+ (NSArray*)retrieveJSONItems;
jsonToArray.m
+ (NSArray*)retrieveJSONItems { //skipped a few boring lines
NSArray *json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url]
options:kNilOptions
error:&err];
return json;}
Job+Json.h //Job- my entity
+ (Job *)appsWithJSONInfo:(NSDictionary *) nbJSONInfo
inManagedObjectContext:(NSManagedObjectContext *)context;
+ (Job *)appsWithID:(NSString *)newID inManagedObjectContext:(NSManagedObjectContext *)context;
Job+Json.m
#import "Apps+JSON.h"
#import "JSONToArray.h"
#implementation Job (JSON)
+ (Job *)appsWithJSONInfo:(NSDictionary *) nbJSONInfo
inManagedObjectContext:(NSManagedObjectContext *)context {
Job *apps = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Job"];
request.predicate = [NSPredicate predicateWithFormat:#"nid=%#", [nbJSONInfo objectForKey:JSON_NID]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches || ([matches count] > 1)) {
[JSONToArray showNetworkError:#"There was an error retrieving the json Data."];
} else if ([matches count] == 0) {
apps = [NSEntityDescription insertNewObjectForEntityForName:#"Job" inManagedObjectContext:context];
apps.nid = NSLocalizedString([nbJSONInfo objectForKey:JSON_NID], nil);
apps.name = NSLocalizedString([nbJSONInfo objectForKey:JSON_NAME], nil);
}else {
apps = [matches lastObject];
}
return apps;
}
+ (Job *)appsWithID:(NSString *)newID
inManagedObjectContext:(NSManagedObjectContext *)context
{
Job *app = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Job"];
request.predicate = [NSPredicate predicateWithFormat:#"nid = %#", newID];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *apps = [context executeFetchRequest:request error:&error];
if ([apps count] == 0) {
app = [NSEntityDescription insertNewObjectForEntityForName:#"Job"
inManagedObjectContext:context];
app.nid = NSLocalizedString(newID, nil);
} else if ([apps count] >=1) {
app = [apps lastObject];
}
return app;
}
And finally my list view:
- (void)viewWillAppear:(BOOL)animated{
[self fetchJSONDataIntoDocument:self.managedObjectContext];}
- (void)fetchJSONDataIntoDocument:(NSManagedObjectContext *)managedObjectContext{
NSArray * tempArray = [JSONToArray retrieveJSONItems];
for (NSDictionary *JSONInfo in tempArray) {
[Job appsWithJSONInfo:JSONInfo inManagedObjectContext:managedObjectContext];
}
NSError *error = nil;
if (![managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
So in this case, I see the two objects on my list view but they are null, with no value. How can I fix that?
Are you developing for > IOS5.0?
Instead of kNilOptions on I use NSJSONReadingMutableLeaves:
[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:&error];

Core data only returning null

I have a number of objects (NSManagedObject Subclass) which 'seem' to be created and saving correctly, however on reloading i can only get null from any of the variables within the objects.
The right number of objects are being saved, but none of the variables.
I've gone through the code several times, and I cant figure out if the problem is on the saving side or the loading side.
Would very much appreciate any thoughts!
Thanks in advance
//Create a route
- (CNSRoute *) createRoute
{
CNSRoute *p = [NSEntityDescription insertNewObjectForEntityForName:#"CNSRoute" inManagedObjectContext:context];
[allRoutes addObject:p];
//NSLog(#"Route: %#", p);
return p;
}
//Load all routes from core data
- (void) loadAllRoutes
{
NSLog(#"All Routes: %#", allRoutes);
if (!allRoutes) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:#"CNSRoute"];
[request setEntity:e];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:#"Fetch Failed" format:#"Reason: %#", [error localizedDescription]];
}
NSLog(#"LOAD RESULT: %#", result);
for (CNSRoute *route in result) {
NSLog(#"Loading Route... %#", [route name]);
}
allRoutes = [[NSMutableArray alloc] init];
[allRoutes setArray:result];
//NSLog(#"%#", allLocations);
}
}
//Save context
- (BOOL)saveChanges
{
NSError *err = nil;
BOOL successful = [context save:&err];
if (!successful) {
NSLog(#"ERROR SAVING: %#", [err localizedDescription]);
}
return successful;
}
//Save when application enters background
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[CNSTravelController sharedStore] saveChanges];
}

Resources