3

I've searched answer here, but none of these solved my problem...
I've saved some data in core data, in Truck Managed Object Subclass. Here's the code:

for (NSDictionary *dictTruck in self.trucksArray) {
        Truck *truck = [NSEntityDescription insertNewObjectForEntityForName:@"Truck" inManagedObjectContext:_managedObjectContext];
        Country *country = [NSEntityDescription insertNewObjectForEntityForName:@"Country" inManagedObjectContext:_managedObjectContext];

        NSFetchRequest *truckRequest = [[NSFetchRequest alloc]init];
        NSEntityDescription *truckDescription = [NSEntityDescription entityForName:@"Truck" inManagedObjectContext:_managedObjectContext];
        [truckRequest setEntity:truckDescription];
        NSMutableArray *mutableReq = [[_managedObjectContext executeFetchRequest:truckRequest error:nil]mutableCopy];
        self.trucksArray = [[NSMutableArray alloc]initWithArray:mutableReq];
        [truck setModel:[dictTruck objectForKey:@"model"]];
        [truck setYear:[dictTruck objectForKey:@"year"]];
        [truck setCountry:country];
        [country setCountryName:[dictTruck objectForKey:@"country"]];
        [_managedObjectContext save:nil];

    }


And this works pretty fine. Now, problem is that I have JSON that I'm downloading from server via AFNetworking in format like this:

{
    "trucks": [
        {
            "model": "Scania",
            "year": "2014",
            "country": "Sweden"
        },
        {
            "model": "DAF",
            "year": "2012",
            "country": "Ireland"
        }
    ]
}


This JSON is updated every hour, so there is old JSON data + new... Now, my question is how can I check if this object exists in Core Data, example: Truck.model = @"Scania", Truck.year = @"2014", Truck.Country.countryName = @"Sweden"; And if it matches all this conditions it should not be saved in CD...
Thanks in advance.
Update:
In this code is piece of request for objects in core data, because I've tried so many ways to solve this.

Stefan
  • 1,283
  • 15
  • 33
  • You need to use NSFetchRequest along with NSPredicate to look up to a specific record: http://stackoverflow.com/questions/10612955/fetch-request-for-entity-attribute-somevalue – user523234 Aug 18 '15 at 08:28
  • Could you be more specific, because I created NSFetchRequest, but problem is that I don't know how to set condition to check if it is satisfied. – Stefan Aug 18 '15 at 08:29
  • That is what the NSPredicate is for. In the link I posted, if the fetched result is negative then you know... I am not at a computer stackoverflow.com I can't write code. – user523234 Aug 18 '15 at 08:32

1 Answers1

2

In my opinion there should be a UNIQUE truck_id in the response json, like this

{
    "trucks": [
        {
            "id": "00000001"
            "model": "Scania",
            "year": "2014",
            "country": "Sweden"
        },
        {
            "id": "00000002"
            "model": "DAF",
            "year": "2012",
            "country": "Ireland"
        }
    ]
}

Save the id to Truck model and every time you get response from server, just check if there is a model with same id in core data.

For every truck json, make a fetch request first

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
request.entity = [NSEntityDescription entityForName:@"Truck" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"truckID = %d", truckIDInResponse];
request.predicate = predicate;
NSError *error = nil;
NSArray *objs = [context executeFetchRequest:request error:&error];
if (error) {
    [NSException raise:@"no truck find" format:@"%@", [error localizedDescription]];
}
if (objs.count > 0) {
    // there is a truck with same id exsist. Use update method
}else {
    // there's no truck with same id. Use insert method
}

The truckID is the property of your Truck model.

zy.liu
  • 346
  • 4
  • 15