- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
设备:iPhone 8、Apple Watch Series 3
我目前正在开发一个手机应用 + watch 扩展,它使用标准 API 和相关的 OperationQueues 从所有 CoreMotion 传感器流式传输 CoreMotion 数据。
我需要能够并行传输所有 CoreMotion 传感器,将样本写入 csvs,然后导出它们。将样本存储在数组中的内存中会按预期导致内存压力问题,所以现在我将每个样本在收到时直接写入文件 - 因此对于 X=# 的流式传感器,我也有 X 个文件被写入,也在相关的操作队列。
我的流式处理代码如下所示(例如,用于加速):
public func start(mm: CMMotionManager, fileQueue: OperationQueue, myOutputURL: URL) {
if mm.isAccelerometerAvailable && !mm.isAccelerometerActive {
mm.startAccelerometerUpdates(to: sampleQueue, withHandler: { (sample: CMLogItem?, error: Error?) in
if let error = error as NSError? {
print("There was a sampling error --- \(error.localizedDescription)")
} else {
if let accelerometerData = sample as? CMAccelerometerData {
let now = Date().timeIntervalSince1970
let sampleComponents: [Double] = [
now,
accelerometerData.timestamp,
accelerometerData.acceleration.x,
accelerometerData.acceleration.y,
accelerometerData.acceleration.z
]
let row = sampleComponents.map({ String(format: "%.5f", $0) })
let sampleString = row.joined(separator: ",") + "\n"
if FileManager.default.fileExists(atPath: myOutputURL.path)
let writeOperation = BlockOperation(block: {
do {
if let utf8Sample = sampleString.data(using: .utf8) {
let fileHandle = try FileHandle(forWritingTo: myOutputURL)
fileHandle.seekToEndOfFile()
fileHandle.write(utf8Sample)
fileHandle.closeFile()
}
} catch let error as NSError {
print("There was an error while writing to file --- \(error.localizedDescription)")
}
})
if let lastOperation = fileQueue.operations.last {
writeOperation.addDependency(lastOperation)
}
fileQueue.addOperation(writeOperation)
print("Num OPs in Sample Queue: \(sampleQueue.operations.count)")
print("Num OPs in I/O Queue: \(fileQueue.operations.count)")
}
}
})
}
}
这很好用……在电话上。采样操作队列在任何给定时间很少有超过 100 个总操作,并且文件队列最多 5 个 - 即使流式传输超过一个小时。然而,我在我的 watchkit 扩展中使用完全相同的代码来传输加速和运动数据,并且发生了一些奇怪的事情。传感器数据的样本队列无限增加。换句话说,就好像来自 CoreMotion 采样的操作永远不会从采样队列中取出。
旁注:除了依赖链,队列还配置了:
sampleQueue.maxConcurrentOperationCount = 1
fileQueue.maxConcurrentOperationCount = 1
希望按顺序接收和编写样本。
这种无限增加导致内存分配在大约 15 分钟的流式传输后接近 80MB,我根据经验发现这是我的 watch 操作系统因压力过大而终止扩展时的近似上限。当每个样本进入时,我已经记录了样本队列中的操作数,当它接近 80MB 标记时,队列中大约有 60-80k 个操作,而在 I/O 队列中,这个数字类似于电话的那个。我不明白为什么,因为更直观的是,瓶颈将存在于磁盘 I/O 队列中,而不是样本队列中,尤其是随着文件大小和样本数量的增加。
我很好奇其他人是否遇到过这样的问题以及他们是如何克服它的,因为这似乎是从 Apple 设备存储和导出传感器数据的一个相当标准的问题。
具体来说,我很好奇其他人如何设法在这些设备上长时间传输和存储传感器数据以供日后分析,以及您是否觉得我做的事情不正确。非常感谢!
最佳答案
我认为这样做的关键是尽可能少地对 watch 本身进行处理。
这并没有直接解决问题,但我所做的是将数据从 Watch 流式传输到 iPhone,并在 iPhone 上完成对 CSV 的处理。这避免了让 watch 做任何繁重的工作(这对于旧 watch 来说更为重要)。
另外,你的 updateInterval 是多少?
关于swift - Apple Watch 上的 CoreMotion OperationQueue 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49181216/
我使用的是 swift 3.0.2。 Xcode 8.3.1 我尝试使用 isSuspended = true 属性,但它不会停止操作。 当我取消一个 operation1 时,所有其他依赖于 ope
在使用异步递归调用 API 时,我无法理解如何使用 GCD。 下面是三个类似方法之一,包含相同的逻辑,只是针对不同的数据和 API 端点。如果没有下一页请求,该方法应该完成并且下一个方法应该开始。 我
我感兴趣的是,当应用收到UIApplicationWillResignActive通知时,是否需要在OperationQueue中调用.cancelAllOperations()? 最佳答案 应用进入
从 iOS13 开始,可以使用 progress 属性监控 OperationQueue 的进度。文档指出,在跟踪进度时,只有不覆盖 start() 的操作才算数。但是,根据文档,异步操作必须覆盖st
我有一个操作队列,我正在调用 cancelAllOpeations 但如果我问OperationQueue.operationcount 它不会让我返回零。 我正在覆盖取消方法,一切正常,但 oper
我正在阅读有关 OperationQueue 的文档。 我有这样的疑问,我们正在创建的操作,BlockOperation 和 OperationQueue 的同步和异步状态是什么。 据我了解,它将作为
我创建了一个继承自基类“Operation”的子类“MyOperation”。我在“MyOperation”中添加了一个函数,该函数在类实例化时被调用。但是,当创建操作类并将其添加到操作队列时,不会调
如果我创建了一个管理器并实例化了,我会这样做: AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRe
我正在做一些冗长的计算以在后台线程上创建图表数据 我本来是用GCD的,但是每次用户通过点击按钮过滤图表数据时,图表数据都需要重新计算,如果用户点击图表数据过滤按钮非常快(高级用户)然后图表循环在每个
DispatchGroup 和 OperationQueue 有方法 wait() 和 waitUntilAllOperationsAreFinished() 等待各自的所有操作排队等待完成。 但即使
我是一名 Android 开发者,目前正在学习 Swift。你能帮我处理 OperationQueue 吗? class ViewController: UIViewController {
我通过 JSON 获得了两种类型的信息,并且我正在使用 addObserver(forKeyPath:"operations"...) 向 2 个不同的操作队列类添加“操作”。在函数 observeV
当您需要在网络任务或操作的完成 block 中的主线程上执行某些操作时,以下哪种方法最合适,为什么?: OperationQueue.main.addOperation DispatchQueue.m
tl;dr 我有一个 OperationQueue,我想同时运行两个操作。这些操作异步下载一些东西,因此它们都会立即被触发,而不是一个接一个地运行。 我通过对每张图片执行以下操作来填充一张非常大的图片
我有一个串行 OperationQueue,其操作调用 usleep。我这样做是因为操作执行 block 与需要重复直到指定时间的计时器同步。 例如,将 3 个操作添加到 maxconcurrent
我陷入了一个概念上简单的问题。发生的事情是解析操作在下载操作的完成处理程序完成之前执行。因此,没有要解析的数据。您可以将以下代码直接放入文件中并运行它。 如何确保在解析操作运行之前完成下载? impo
设备:iPhone 8、Apple Watch Series 3 我目前正在开发一个手机应用 + watch 扩展,它使用标准 API 和相关的 OperationQueues 从所有 CoreMot
iOS 13 在 OperationQueue 类中引入了 progress 属性。同时,Apple 将 operations 和 operationCount 属性标记为已弃用,这表明它们不应再用于
有时我必须在主线程上执行某些操作,建议将代码放置在 OperationQueue.main.addOperation 中。 其他时候,建议将代码编写在DispatchQueue.main.async内
我在处理许多图像的管道中有 2 个步骤: 第 1 步:在本地加载(或下载)图像(IO 绑定(bind)) 第 2 步:运行机器学习模型(CPU/GPU/受计算限制/单线程,因为模型很大)。如何限制存储
我是一名优秀的程序员,十分优秀!