相关疑难解决方法(0)

保持NSThread存活并在其上运行NSRunLoop

所以我正在开始一个新的NSThread,我希望以后可以通过调用来使用它performSelector:onThread:....根据我的理解,调用方法将该调用添加到该线程上的runloop,因此在下一次迭代时,它将弹出所有这些调用,然后调用它们,直到没有任何东西可以调用.所以我需要这种功能,一个空闲的线程准备工作,我可以调用它.我当前的代码如下所示:

   - (void)doInitialize
   {
       mThread =  [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
       [mthread start];
   }

   - (void)runThread
   {
       NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

       // From what I understand from the Google machine is that this call should start the
       // runloop on this thread, but it DOESN'T. The thread dies and is un-callable
       [[NSRunLoop currentRunLoop] run];

       [pool drain];
   }

   - (void)scheduleSomethingOnThread
   {
       [self performSelector:@selector(hardWork) onThread:mThread withObject:nil waitUntilDone:NO];
   }
Run Code Online (Sandbox Code Playgroud)

但是线程没有保持活动状态,performSelector:onThread没有做任何事情.我该怎么做正确的方法?

cocoa objective-c foundation nsthread nsrunloop

22
推荐指数
2
解决办法
1万
查看次数

在后台线程上接收CLLocation更新

我正在尝试使用iPhone SDK实现(非并发)NSOperation以进行位置更新.NSOperation子类的"肉"是这样的:

- (void)start {
    // background thread set up by the NSOperationQueue
    assert(![NSThread isMainThread]);

    if ([self isCancelled]) {
        return;
    }

    self->locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = self->desiredAccuracy;
    locationManager.distanceFilter = self->filter;
    [locationManager startUpdatingLocation];

    [self willChangeValueForKey:@"isExecuting"];
    self->acquiringLocation = YES;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)cancel {
    if ( ! self->cancelled ) {
        [self willChangeValueForKey:@"isCancelled"];
        self->cancelled = YES;
        [self didChangeValueForKey:@"isCancelled"];

        [self stopUpdatingLocation];
    }
}

- (BOOL)isExecuting {
    return self->acquiringLocation == YES;
}

- (BOOL)isConcurrent {
    return NO;
}

- …
Run Code Online (Sandbox Code Playgroud)

iphone core-location cllocationmanager

4
推荐指数
1
解决办法
2723
查看次数