我不知道如何處理髮生在我身上的問題。將核心數據與服務器響應同步
我有一個應用程序正在使用AFNetworking
從服務器獲得JSON
,我使用Core Data
將其放入SQLite
。但每次啓動應用程序時,如果服務器上存在一些新對象並與設備上的本地對象同步,則必須進行chceck操作。 如何處理服務器和應用程序之間的這種同步?
的方法之一是再次明確整個數據庫和下載的一切,但我有一個屬性,其持有的對象(選擇)的狀態,我不能失去這個信息。我得到了一個建議,將響應與當前的數據庫同步,但我真的不知道如何有效且智能地實現這一點。
我唯一的想法是獲取所有內容,並將每個對象從響應中與整個NSArray
進行比較。如果匹配,則循環中斷,如果沒有添加對象。
我用這種方法得到的一切,從服務器,並把它放到數據庫:
[[AFHTTPRequestOperationManager manager] GET:ALL_GROUPS parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
for (id group in responseObject) {
Group *groupEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Group" inManagedObjectContext:self.moc];
groupEntity.name = [group valueForKey:@"name"];
groupEntity.cashID = [group valueForKey:@"id"];
groupEntity.caseInsensitiveName = [[group valueForKey:@"name"] lowercaseString];
groupEntity.selected = FALSE;
NSError *error;
if (![self.moc save:&error]) {
NSLog(@"Couldn't save: %@", [error localizedDescription]);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Show alert with info about internet connection
UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle:@"Ups!" message:@"Wygląda na to, że nie masz połączenia z internetem" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[internetAlert show];
}];
而且這個小傢伙來獲取一切:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Group"
inManagedObjectContext:self.moc];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [self.moc executeFetchRequest:fetchRequest error:&error];
我將不勝感激任何建議.. 。我堅持使用這一個...
用戶可以更改下載到設備的數據嗎? – CarmeloS
@CarmeloS,只有'selected'屬性可以。 – cojoj
由AFHTTPNetworking實現的NSIncrementalStore,你也可以看看它:https://github.com/AFNetworking/AFIncrementalStore –