3
@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];

そしてそれは機能していません、それはなぜですか?

4

1 に答える 1

4

私はそれを考え出した。2番目のバージョンでは、作成していたCMMotionManagerインスタンスが何らかの理由で失われました。したがって、MotionHandlerクラスの実装を次のように変更しました。

MotionHandler.m

@interface MotionHandler()
{
   //..
   CMMotionManager *motionManager; // I now declare it here
}

@implementation MotionHandler

@synthesize accelerationOnYaxis; // this is an int property of the MotionHandler class

-(void) startAccelerationUpdates
{
    motionManager = [[CMMotionManager alloc] init]; // and then initialize it here..
    motionManager.deviceMotionUpdateInterval = 0.01;
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                                       withHandler:^(CMDeviceMotion *motion, NSError *error)
                                       {
                                           self.accelerationOnYaxis = motion.userAcceleration.y;
                                       }
    ];
}
-(void)test
{
    [self startAccelerationUpdates];
    while(1==1)
    {
        NSLog(@"current acceleration on y-axis is: %f", self.accelerationOnYaxis);
    }
}

そして今、それはうまく機能しているようです。

于 2012-09-03T19:28:13.390 に答える