更新时间:2023-11-25 11:09:52
在我看来,在应用程序中应该有 UNIQUE truck_id
响应json,像这样
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"
}
]
}
保存 id $ c>到
到
模型,并且每次从服务器获取响应时,只需检查是否存在具有相同
id
。
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.
对于每个卡车json,首先提出获取请求
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
}
truckID
是卡车
模型的属性。