我有一个在应用程序的主线程中初始化的对象,但我在后台线程中使用performSelectorInBakground
. 但是,该方法似乎没有响应。进一步来说 :
AccHandler.m
#import "AccHandler.h"
@implementation AccHandler
@synthesize accelerationOnYaxis;
-(void)startAccelerationUpdates
{
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 0.01;
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
self.accelerationOnYaxis = motion.userAcceleration.y;
}
];
}
@end
测试者.m
#import "Tester.h"
@interface Tester()
{
AccHandler *accHandler;
}
@end
@implementation Tester
- (id) init
{
//....
accHandler = [[AccHandler alloc] init]; // initialized in main thread
}
-(void)test
{
[accHandler startAccelerationUpdates]; // but -(void)test will be executed in a background thread..
NSLog(@"current acceleration is %f", accHandler.accelerationOnYaxis;
}
稍后在我的代码中,我test
从 a 调用该方法,performSelectorInBackground
但我得到 0 加速度值..