@interface Tester()
{
int currentAccelerationOnYaxis;
}
@end
@implementation Tester
-(void) test
{
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 0.01;
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
currentAccelerationOnYaxis = motion.userAcceleration.y;
}
];
while(1==1)
{
NSLog(@"current acceleration is: %f", currentAccelerationOnYaxis);
}
}
@end
次に、次のようなバックグラウンドスレッドで上記のメソッドを実行します。
[myTester performSelectorInBackground:@selector(test) withObject:nil];
そしてそれはうまくいきます。
ただし、次の構成が機能しておらず、理由がわかりません。
@implementation MotionHandler
@synthesize accelerationOnYaxis; // this is an int property of the MotionHandler class
-(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;
}
];
}
@implementation Tester
-(id)init
{
//...
currentMotionHandler = [[MotionHandler alloc] init];
}
-(void) test
{
[currentMotionHandler startAccelerationUpdates];
while(1==1)
{
NSLog(@"current acceleration is: %f", currentMotionHandler.accelerationOnYaxis);
}
}
@end
次に、次のようなバックグラウンドスレッドで上記のメソッドを実行します。
[myTester performSelectorInBackground:@selector(test) withObject:nil];
そしてそれは機能していません、それはなぜですか?