gpt4 book ai didi

ios - NSFetchedresultsController 部分 Name KeyPath 基于当前位置和其他位置之间的范围

转载 作者:行者123 更新时间:2023-11-29 03:01:33 25 4
gpt4 key购买 nike

我有一个包含对象的实体。这些对象具有 Latitudelongitude。我还添加了一个 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]];

这工作正常,我只得到 5 公里半径 内的对象。在我的 tableView 底部,我有一个 load more 按钮,我在其中更改了 _searchDistance(我又添加了 5km)

为了构建我的列表,我使用了这个 NSFetchedresultController:

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context sectionNameKeyPath:@"range"
cacheName:nil];

问题

当 View 加载时,一切正常。但是当我点击加载更多时,我得到一个空列表并在我的日志中得到这个错误

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'";

任何帮助将不胜感激!提前致谢!

最佳答案

问题在于所有对象首先根据第一个排序描述符进行排序,然后根据 sectionNameKeyPath 分组为多个部分。documentation关于此参数的说明:

If this key path is not the same as that specified by the first sort descriptor in fetchRequest, they must generate the same relative orderings.

这意味着您必须使用第一个排序描述符来对所有对象进行排序根据他们到当前位置的距离。不幸的是,这种排序描述符只能使用 persistent 属性。但是(据我所知)没有办法解决这个问题如果您使用获取的结果 Controller ,则限制。

关于ios - NSFetchedresultsController 部分 Name KeyPath 基于当前位置和其他位置之间的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23241478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com