1

我查看了 AFIncreementalStore 的 gitHub 页面上包含的示例,并认为我了解从http://apiurl.com/users之类的 URL 从远程 api 请求对象的过程。基本上我会,例如在 UsersListViewController 中,这样做:

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"userID" ascending:YES]];
    fetchRequest.returnsObjectsAsFaults = NO;

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[(id)[[UIApplication sharedApplication] delegate] managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
    _fetchedResultsController.delegate = self;
    [_fetchedResultsController performFetch:nil];

我很难弄清楚如何从诸如http://apiurl.com/users/1/posts之类的 URL 中获取诸如“帖子”之类的内容。

我最初的想法是在 fetch 请求中添加一个谓词:

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Post"];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:YES]];
    fetchRequest.returnsObjectsAsFaults = NO;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID == %@", userID];        
    [request setPredicate:predicate];

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[(id)[[UIApplication sharedApplication] delegate] managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
    _fetchedResultsController.delegate = self;
    [_fetchedResultsController performFetch:nil];

添加这个谓词会使用这个 URL 生成一个 GET 请求(假设我在谓词中使用的用户 ID 是 1)?http://apiurl.com/users/1/posts

4

0 回答 0