gpt4 book ai didi

ios - 在RestKit中,如何强制本地数据库匹配远程 "master"数据集

转载 作者:行者123 更新时间:2023-11-29 04:45:21 25 4
gpt4 key购买 nike

是否有一种简单的方法可以强制 RestKit 在与“主”远程数据集同步时删除旧的本地项目?

那么本质上是删除新数据集中找不到的元素吗?

最佳答案

如果您使用的是 RestKit 0.10,我刚刚添加了:

[objectManager.mappingProvider setObjectMapping:eventsMapping
forResourcePathPattern:@"data.xml"
withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
return [Events fetchRequest];
}];

到我现有的代码。现在更新时,服务器上删除的任何对象都会从 RestKit 的托管对象中删除。

这是整个序列:

// Set up the opjectManager
objectManager = [RKObjectManager objectManagerWithBaseURLString:kBaseUrlString];
objectManager.client.disableCertificateValidation = YES; // Using self signed cert
objectManager.client.cachePolicy = RKRequestCachePolicyLoadIfOffline |
RKRequestCachePolicyTimeout |
RKRequestCachePolicyLoadOnError |
RKRequestCachePolicyEtag;
objectManager.client.requestCache.storagePolicy = RKRequestCacheStoragePolicyPermanently;
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;

// Set up the objectStore
RKManagedObjectStore* objectStore = [RKManagedObjectStore
objectStoreWithStoreFilename:databaseName
usingSeedDatabaseName:seedDatabaseName
managedObjectModel:nil
delegate:self];
objectStore.delegate = self;
objectStore.cacheStrategy = [RKInMemoryManagedObjectCache new];

// Set up the event mapping (in my case, this is an XML file)
RKManagedObjectMapping *eventsMapping = [RKManagedObjectMapping mappingForEntityWithName:kEntityEvents
inManagedObjectStore:objectStore];
[eventsMapping mapKeyPath:@"contact" toAttribute:@"contact"];
[eventsMapping mapKeyPath:@"startDate" toAttribute:@"startDate"];
[eventsMapping mapKeyPath:@"endDate" toAttribute:@"endDate"];
[eventsMapping mapKeyPath:@"icon" toAttribute:@"icon"];
[eventsMapping mapKeyPath:@"location" toAttribute:@"location"];
[eventsMapping mapKeyPath:@"name" toAttribute:@"name"];
[eventsMapping mapKeyPath:@"notes" toAttribute:@"notes"];
[eventsMapping mapKeyPath:@"section" toAttribute:@"section"];
[eventsMapping mapKeyPath:@"url" toAttribute:@"url"];
eventsMapping.primaryKeyAttribute = @"name";

[objectManager.mappingProvider registerMapping:eventsMapping
withRootKeyPath:@"root.events.event"];

// Added for RestKit upgrade from 0.9 to 0.10 to delete orphans (records not in GET)
[objectManager.mappingProvider setObjectMapping:eventsMapping
forResourcePathPattern:@"data.xml"
withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
return [Events fetchRequest];
}];

// Load the opjects from XML file
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"data.xml"
delegate:self];

关于ios - 在RestKit中,如何强制本地数据库匹配远程 "master"数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com