【发布时间】:2014-04-23 10:23:47
【问题描述】:
我有一个entity,里面有对象。这些对象有Latitude 和longitude。我还添加了一个 transient attribute range 来计算范围。请看下面的类:
@implementation Relation
@dynamic firstLetter;
@dynamic rel_address;
@dynamic rel_balanceTotal;
@dynamic rel_bank_country_code;
@dynamic rel_bank_number;
@dynamic rel_city;
@dynamic rel_city_id;
@dynamic rel_code;
@dynamic rel_country;
@dynamic rel_country_code;
@dynamic rel_customerProspect;
@dynamic rel_email;
@dynamic rel_expired_total;
@dynamic rel_fax;
@dynamic rel_gsm;
@dynamic rel_name;
@dynamic rel_phone;
@dynamic rel_turnovertotal;
@dynamic rel_vat_country_code;
@dynamic rel_vat_number;
@dynamic rel_website;
@dynamic rel_zipcode;
@dynamic rel_longitude;
@dynamic rel_latitude;
@dynamic range;
-(NSNumber *)range{
NSNumber *rangeFromCurrentLocation;
[self willAccessValueForKey:@"range"];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
CLLocation *location = [[CLLocation alloc]initWithLatitude:[self.rel_latitude floatValue] longitude:[self.rel_longitude floatValue]];
CLLocation *location2 = [[CLLocation alloc]initWithLatitude:appDelegate.latitude longitude:appDelegate.longitude];
CLLocationDistance distance = [location2 distanceFromLocation:location]; // distance is expressed in meters
CLLocationDistance kilometers = distance / 1000.0;
float floatrange = ceil(kilometers / 5.0) * 5;
rangeFromCurrentLocation = [NSNumber numberWithFloat:floatrange];
[self didAccessValueForKey:@"range"];
NSLog(@"RANGE IS %@",rangeFromCurrentLocation);
return rangeFromCurrentLocation;
}
我想要做的是建立一个包含 5 公里、10 公里、15 公里、...的路段的列表。 在启动时,我只加载半径为 5 公里的关系。我就是这样做的。
_searchDistance = 2.50;
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
float minLat = appDelegate.latitude - (_searchDistance / 69);
float maxLat = appDelegate.latitude + (_searchDistance / 69);
float minLon = appDelegate.longitude - _searchDistance / fabs(cos(appDelegate.longitude / 180.0 * M_PI)*69);
float maxLon = appDelegate.longitude + _searchDistance / fabs(cos(appDelegate.longitude / 180.0 * M_PI)*69);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Relation" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rel_latitude <= %f AND rel_latitude >= %f AND rel_longitude <= %f AND rel_longitude >= %f", maxLat, minLat, maxLon, minLon];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"rel_name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort,nil]];
这工作正常,我只得到radius of 5km 中的对象。在我的 tableView 的底部,我有一个 load more button,我在其中更改了 _searchDistance(我又增加了 5 公里)
为了建立我的列表,我使用这个NSFetchedresultController:
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context sectionNameKeyPath:@"range"
cacheName:nil];
问题
当视图加载时,一切正常。但是当我点击加载更多时,我得到一个空列表并在我的日志中得到这个错误
CoreData: error: (NSFetchedResultsController) The fetched object at index 6 has an out of order section name '10. Objects must be sorted by section name'
2014-04-23 12:01:27.673 Adsolut[2097:60b] Unresolved error Error Domain=NSCocoaErrorDomain Code=134060 "The operation couldn’t be completed. (Cocoa error 134060.)" UserInfo=0x167630f0 {reason=The fetched object at index 6 has an out of order section name '10. Objects must be sorted by section name'}, {
reason = "The fetched object at index 6 has an out of order section name '10. Objects must be sorted by section name'";
任何帮助将不胜感激! 提前致谢!
【问题讨论】:
标签: ios iphone objective-c core-data nsfetchedresultscontroller