gpt4 book ai didi

ios - 在单独的线程中循环位置数组

转载 作者:行者123 更新时间:2023-11-28 22:01:04 25 4
gpt4 key购买 nike

我正在为 iPhone 开发一个应用程序,我正在跟踪用户的当前位置。当 didupdateLocations 委托(delegate)方法实际执行时,我想测试 NSArray 中的位置是否在包含其他位置的预定义数组中,这个数组会随着时间的推移而增长。

我正在这个方法中运行一个 for 循环来测试我自己的位置数组,但我想将它移到一个单独的线程中。因此,如果我自己的具有多个位置的数组增长到很大数量,for 循环不会卡住我的 UI。我试过这样但我得到了不希望的结果。我知道位置跟踪肯定发生在一个单独的线程中。但是,那些 didupdateLocations 在单独的线程上执行。苹果文档对此事不是很清楚。我的最终目标还是与我的数组进行比较而不是锁定 UI。

  - (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)thisLocation {

dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

// get the last object in the array of locations
CLLocation* location = [thisLocation lastObject];

dispatch_async(queue, ^{
[self checkmyArray:location];
});

}



-(void)checkmyArray:(CLLocation *)workingLocation{

NSLog(@"SoundTheAlarm");
int alarm_on_c = 0;
NSUInteger tmp_count = [theData.LocationsObjectArray count];
BOOL alarm;
NSMutableDictionary * tempObject;
CLLocationDistance distance = 0.0;


for (int i = 0; i < tmp_count; i++) {

tempObject= [theData.LocationsObjectArray objectAtIndex:i];

thisLoc = [[tempObject objectForKey:@"onoff"] isEqual:@YES];


if (thisLoc) {


//check if we are near that location
double lat = [[tempObject objectForKey:@"latitude"] doubleValue];
double lon = [[tempObject objectForKey:@"longitude"] doubleValue];

// goal = [[CLLocation alloc] initWithLatitude:40.097771 longitude:-74.941399];
goal = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

// check the destination between current location and goal location - in meters
distance = [goal distanceFromLocation:workingLocation];

NSLog(@"distance %f\n", distance);
}


// if distance from goal is less than 350 meters
if (distance <= 350){

[self scheduleNotification:[tempObject objectForKey:@"name"]];

// turn off tracking for this location
[tempObject setObject:@NO forKey:@"onoff"];
[theData.LocationsObjectArray replaceObjectAtIndex:i withObject:tempObject];

NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
ExtendedSavedCellTableViewCell *cell = (ExtendedSavedCellTableViewCell *)[self.tableView cellForRowAtIndexPath:path];
cell.switchView.on = NO;

// save the update to the switch to the database as well
NSString *lat = [tempObject objectForKey:@"latitude"];

/*check to determine if the uiswitch is turned off or on.*/

[self fetchedResultsController:@NO lat:lat index:path];
[self displayAlertViewForAlarm:[tempObject objectForKey:@"name"]];


}

-(void)displayAlertViewForAlarm:(NSString *)nameOfLocation{

dispatch_async(dispatch_get_main_queue(), ^(void) {


UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Destination reached"
message:nameOfLocation
delegate:self
cancelButtonTitle:@"Go Away"
otherButtonTitles:@"Notify", nil];


[myAlert show];
});



}

最佳答案

如果可以完全避免的话,在 iOS 中使用线程通常不是一个好主意。在您的情况下,我将实现执行循环的函数,以便在太多次迭代后自动弹出循环,然后安排下一个迭代 block 在事件循环的另一次传递中发生。换句话说,像这样:

- (void) checkLocationsStartingAt:(NSNumber)start
{
NSInteger s = (start) ? [start intValue] : 0;
for (i = s; i < list.length; i++) {
if (i > TOO_MANY) {
[self performSelector:@selector(checkLocationsStartingAt:)
withObject:@(i)
afterDelay:0.001];
return;
} else {
// check element at i
}
}
}

参见:NSObject Reference

关于ios - 在单独的线程中循环位置数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25086047/

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