我有一个名为Route
的实体和来自该实体的两个名为Longitude
和Latitude
的属性。
应用程序的功能很简单:它将从Longitude
和Latitude
获取数据并创建NSArray。
例如:
Route 1 has Longitude=2 and Latitude=41
Route 2 has Longitude=3 and Latitude=42
Route 3 has Longitude=4 and Latitude=43
因此,结果将是具有该内容的NSArray:
{
[[CLLocation alloc] initWithLatitude:41 longitude:2],
[[CLLocation alloc] initWithLatitude:42 longitude:3],
[[CLLocation alloc] initWithLatitude:43 longitude:4],
}
但问题是如果我从不同的setupFetchedResultsController中获取这两个属性,我就可以"连接"他们与合适的合作伙伴(我将获得2个分开的值列表)。是否有另一种方法可以从2个属性中获取数据"已连接"?
如果有人需要查看,这是我的fetchedResultsController
之一:
- (NSFetchedResultsController *)fetchedLatitudController
{
if (_fetchedLatitudController != nil) {
return _fetchedLatitudController;
}
// 1 - Decide what Entity you want
NSString *entityName = @"Route"; // Put your entity name here
// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"latitude"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
// 5 - Fetch it
self.fetchedLatitudController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self.fetchedLatitudController performFetch:nil];
return _fetchedLatitudController;
}
答案 0 :(得分:0)
无需使用两个获取的结果控制器。获取的结果控制器 (默认情况下)获取托管对象子类的对象,然后可以查询这些对象 对于所有属性。例如:
Route *route = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSNumber *lng = route.longitude;
NSNumber *lat = route.latitude;
但请注意,获取的结果控制器通常与表视图一起使用(或 集合视图)。如果您只想创建一个数组,那么您可以执行一个简单的操作 取而代之的是获取请求:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Route"];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:request error:&error];
if (fetchedObjects == nil) {
// Some error occurred ...
} else {
for (Route *route in fetchedObjects) {
NSNumber *lng = route.longitude;
NSNumber *lat = route.latitude;
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lng doubleValue]];
// ... add loc to your array ...
}
}
如果"路线"实体有许多属性,那么它可能更有效 设置
[request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:@[@"longitude", @"latitude"]];
只获取您感兴趣的属性。然后fetchedObjects
是
一组词典,您可以从中创建CLLocation
。