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.