1

我正在尝试开发一个 watchOS 应用程序,它以非常高的频率(100Hz)收集传感器(加速器)数据。我正在使用 Core Data 存储数据并将其导出为 CSV 文件,然后发送到 iPhone。

应用程序在短时间内崩溃,因为某些线程的内存超出了限制。而且我还发现“performBackgroundTask”方法创建的线程太多,CPU使用率超过100%。

另一个问题是当我取出数据时,我得到的数据项比实际存储在 Sqlite 数据库中的数据项多,这很令人困惑。

下面是代码,该方法在这个method( [self.motionManager startDeviceMotionUpdatesToQueue:self.queue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {}])的回调块中被调用:

- (void)workoutManager:(nonnull id)manager didReceiveAccelerationData:(nonnull YLAcceleration *)acceleration {

    NSString *dateTime = [self.dateFormatter stringFromDate:[NSDate date]];
    [self.coreDataStack.persistentContainer performBackgroundTask:^(NSManagedObjectContext * _Nonnull context) {
        Acceleration *accData = [[Acceleration alloc] initWithContext:context];
        accData.x = acceleration.xString;
        accData.y = acceleration.yString;
        accData.z = acceleration.zString;
        accData.heartRate = self.heartRate;
        accData.dateTime = dateTime;
        accData.session = self.currentSession;
        NSError *saveError = nil;
        [context save:&saveError];
        if (error) {
            NSLog(@"Save data failed:%@", error);
        }else{
            NSLog(@"Save data succeeded:%@", [NSThread currentThread]);
        }
    }];

}

先感谢您。

4

0 回答 0