有没有办法检查 NSPredicate 对象以将其序列化为 URL?我正在尝试远程检索数据,并且需要将谓词对象转换为带有服务器理解的查询字符串参数的 URL。
这是受 WWDC 2010 中的一个名为“构建服务器驱动的用户体验”的演讲启发的,演讲者在演讲中谈到了使用 Core-Data 和服务器后端。我已经关注了会议视频和幻灯片,但被困在序列化点上。例如,有一个Person
对象,我试图获取所有名字为“John”的人。我正在使用NSManagedObjectContext
被调用的子类RemoteManagedObjectContext
,它覆盖了该executeFetchRequest
方法,并且应该将调用发送到服务器。提取请求被创建为(省略的非必要部分):
@implementation PeopleViewController
- (NSArray *)getPeople {
RemoteFetchRequest *fetchRequest = [[RemoteFetchRequest alloc] init];
NSEntityDescription *entity = ...
NSPredicate *template = [NSPredicate predicateWithFormat:
@"name == $NAME AND endpoint = $ENDPOINT"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:...];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
// the custom subclass of NSManagedObjectContext executes this
return [remoteMOC executeFetchRequest:fetchRequest error:&error];
}
@end
现在在 的自定义子类中NSManagedObjectContext
,如何将获取请求序列化为适合服务器的查询字符串参数。因此,鉴于上述获取请求,相应的 URL 将是:
http://example.com/people?name=John
可以获得返回的谓词的字符串表示,
name == "John" AND endpoint == "people"
我可以解析以获取参数name
,并且endpoint
。但是,是否可以在不解析字符串的情况下做到这一点?RemoteManagedObjectContext
这是该类的部分实现。
@implementation RemoteManagedObjectContext
- (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error {
// this gives name == "John" AND endpoint == "people"
// don't know how else to retrieve the predicate data
NSLog(@"%@", [[request predicate] predicateFormat]);
...
}
@end