gpt4 book ai didi

iOS 未从返回的 block 中看到预期值

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

我有以下代码,但我从未看到 runStepCountTotal 最后返回。总增量,但最后一个 NSLog 行实际上是在 block 完成之前返回的。

我意识到我错误地使用了该 block ,但想知道是否有人愿意向我解释如何实现这一目标?

非常感谢。

- (NSInteger)getRunSteps
{
__block NSInteger runstepCountTotal = 0;

if([CMMotionActivityManager isActivityAvailable])
{
CMMotionActivityManager *cm = [[CMMotionActivityManager alloc] init];
CMStepCounter *sc = [[CMStepCounter alloc] init];
NSDate *today = [NSDate date];
[cm queryActivityStartingFromDate:[self startDateOf6DayAgo] toDate:today toQueue:[NSOperationQueue mainQueue] withHandler:^(NSArray *activities, NSError *error){

for(int i=0;i<[activities count]-1;i++)
{
CMMotionActivity *a = [activities objectAtIndex:i];
if (a.running)
{
[sc queryStepCountStartingFrom:a.startDate to:[[activities objectAtIndex:i+1] startDate] toQueue:[NSOperationQueue mainQueue] withHandler:^(NSInteger numberOfSteps, NSError *error)
{
runstepCountTotal = runstepCountTotal+numberOfSteps;
NSLog(@"Current Total is %ld",(long)runstepCountTotal);
}];
}
}
}];
}
NSLog(@"Final Total is %ld",(long)runstepCountTotal);
return runstepCountTotal;
}

最佳答案

阅读有关 queryActivityStartingFromDate:toDate:toQueue:withHandler: 方法的文档。

Discussion
This method runs asynchronously, returning immediately and delivering the results to the specified handler block.

您的方法在调用和完成处理程序 block 之前很久就返回了。

关于iOS 未从返回的 block 中看到预期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21197392/

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