我正在開發一個應用程序,在其中我想對服務器進行遠程搜索。我想RestKit將檢索到的數據保存到數據庫。我首先執行本地搜索(當前有效),然後我想進行遠程搜索,然後使用新結果更新表視圖。我有兩個問題iOS,使用RestKit進行遠程服務器搜索
,1應該怎麼我的映射看起來和2 json的返回與兩種不同類型的對象的數組。
的URL看起來是這樣的:
search.json?search=[search string]
它返回的JSON看起來是這樣的:
[
{
"event": {
"id": 2,
[...]
},
{
"news": {
"id": 16,
[...]
}
凡事件和新聞是兩種對象。
在我的應用我有三個型號,郵政(抽象的實體和超類)NewsPost(子類來郵)和事件(子類來郵)。
我的映射是這樣的:
RKManagedObjectMapping* newsMapping = [RKManagedObjectMapping mappingForClass:[NewsPost class] inManagedObjectStore:objectManager.objectStore];
newsMapping.primaryKeyAttribute = @"newsId";
newsMapping.rootKeyPath = @"news";
[newsMapping mapKeyPath:@"id" toAttribute:@"newsId"];
RKManagedObjectMapping *eventMapping = [RKManagedObjectMapping mappingForClass:[CalendarEvent class] inManagedObjectStore:objectManager.objectStore];
eventMapping.primaryKeyAttribute = @"calendarId";
eventMapping.rootKeyPath = @"calendars";
[eventMapping mapKeyPath:@"id" toAttribute:@"calendarId"];
// These two works.
[objectManager.mappingProvider setObjectMapping:newsMapping forResourcePathPattern:@"/package_components/1/news"];
[objectManager.mappingProvider setObjectMapping:eventMapping forResourcePathPattern:@"/package_components/1/calendars"];
// I don't know how these should look/work.
// Since the search word can change
[objectManager.mappingProvider setObjectMapping:eventMapping forResourcePathPattern:@"/package_components/1/search\\.json?search="];
[objectManager.mappingProvider setObjectMapping:newsMapping forResourcePathPattern:@"/package_components/1/search\\.json?search="];
我的搜索代碼如下所示(本地搜索作品):
- (void)setUpSearch
{
if (self.searchField.text != nil) {
[self.posts removeAllObjects];
[self.events removeAllObjects];
[self.news removeAllObjects];
// Search predicates.
// Performs local search.
NSPredicate *contactNamePredicate = [NSPredicate predicateWithFormat:@"contactName contains[cd] %@", self.searchField.text];
NSPredicate *contactDepartmentPredicate = [NSPredicate predicateWithFormat:@"contactDepartment contains[cd] %@", self.searchField.text];
[...]
NSArray *predicatesArray = [NSArray arrayWithObjects:contactNamePredicate, contactDepartmentPredicate, contactEmailPredicate, contactPhonePredicate, linkPredicate, titlePredicate, nil];
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicatesArray];
self.posts = [[Post findAllWithPredicate:predicate] mutableCopy];
if (self.posts.count != 0) {
self.noResultsLabel.hidden = YES;
for (int i = 0; i < self.posts.count; i++) {
Post * post = [self.posts objectAtIndex:i];
if (post.calendarEvent == YES) {
[self.events addObject:post];
} else {
[self.news addObject:post];
}
}
}
// reload the table view
[self.tableView reloadData];
[self performRemoteSearch];
}
}
- (void)search
{
[self setUpSearch];
[self hideKeyboard];
[self performRemoteSearch];
}
- (void)performRemoteSearch
{
// Should load the objects from JSON
// Note that the searchPath can vary depending on search text.
NSString *searchPath = [NSString stringWithFormat:@"/package_components/1/search.json?search=%@", self.searchField.text];
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:searchPath delegate:self];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
{
// This never gets called.
// Should update my arrays and then update the tableview, but it never gets called.
// Instead I get Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Could not find an object mapping for keyPath: ''
}
如何我應該還是可以做任何提示,將不勝感激。
嗨,我想試試這個問題。 1.你能夠以任何方式更改JSON的格式嗎?我想知道你是否可以爲每個結果添加一個根密鑰路徑(這將大大簡化它)。 2.您正在使用NSManagedObject進行新聞和事件。您是否打算在用戶搜索時將映射保存到Coredata中? – 2012-08-15 10:18:32
嗨,是的,如果需要我可以改變JSON。而且,是的,我想在用戶搜索時在本地緩存並保存結果。 – Anders 2012-08-15 10:44:42
讓我知道答案是否適合你。 – 2012-08-17 06:33:19