简体   繁体   English

关于可可程序的程序布局和存储的建议,以进行分析

[英]Suggestions on program layout and storage of Cocoa program that analyzes

Short background: I am currently writing a program in Xcode for the Mac, which I plan to take parts of (conceptually, if not whole chunks of the code) over to the iPhone. 背景简介:我目前正在为Mac用Xcode编写一个程序,我打算将其中的一部分(如果不是完整的代码块的话,部分地放在iPhone上)。 It involves constantly receiving data through bluetooth from a external sensor (regardless of user interaction the data must be received). 它涉及不断地通过蓝牙从外部传感器接收数据(无论用户如何交互,都必须接收数据)。 I've built a simple program on the Mac using IOBluetooth that pairs and starts receiving the data just fine, and I plan on using BTstack and a jailbroken iPhone in order to access the bluetooth chip on the iPhone. 我已经在Mac上使用IOBluetooth构建了一个简单的程序,该程序可以配对并开始很好地接收数据,并且我打算使用BTstack和越狱的iPhone来访问iPhone上的蓝牙芯片。

Before I get too far I want to conceptually lay this program out correctly, because I am used to procedural programming and Obj-C is a new beast for me. 在开始之前,我想从概念上正确地布局该程序,因为我已经习惯了过程编程,而Obj-C对我来说是新的野兽。 As I stated, I would like to be able to save as much of this code as possible when I move to the iPhone (I understand there are different classes for views etc, but I see -lots- of similarities). 就像我说过的那样,当我移到iPhone时,我希望能够保存尽可能多的这段代码(我知道视图等有不同的类,但是我看到很多相似之处)。

1) With my program I will be constantly receiving data in the background (regardless of user actions - ie, once the user starts the program and picks the BT device, the data will flow) and I need to store and analyze that data before it can be presented to the user. 1)使用我的程序,我将不断在后台接收数据(无论用户采取何种行动,即,一旦用户启动程序并选择BT设备,数据就会流动),我需要先存储并分析该数据可以呈现给用户。 So (the question), how would one lay this out? 因此(问题),如何布置? I was thinking of putting all of my BT code in the appdelegate, and then having a view controller (on the mac would just be one which handles the window, but on the iPhone would be a tab controller with multiple sub view controllers), and a model that analyzes and stores the data (also as log files, for future reference) that is accessed by the "controller", in this case the appdelegate. 我当时正在考虑将所有BT代码放入appdelegate中,然后使用一个视图控制器(在Mac上将是一个处理窗口的控制器,而在iPhone上将是一个带有多个子视图控制器的选项卡控制器),并且一个模型,该模型分析并存储“控制器”(在本例中为appdelegate)访问的数据(也作为日志文件,以备将来参考)。 Does this layout make sense? 这种布局有意义吗? Is it kosher MVC/Cocoa to put all of the BT code and analysis in appdelegate, or should it(they) be in its own class(es) (knowing the BT code on both the mac and iPhone must constantly receive bursts of data)? 将所有BT代码和分析放到appdelegate中是犹太MVC / Cocoa,还是应该属于自己的类(知道Mac和iPhone上的BT代码必须不断接收突发数据) ? How could it be improved? 如何改善?

2) A related question on the analysis side. 2)分析方面的一个相关问题。 I haven't found a single Cocoa example on the net that has analysis (I've found programs, but no explanation of the model they use). 我没有在网上找到可分析的单个Cocoa示例(我找到了程序,但没有解释它们使用的模型)。 The basic data that is saved is very small ~50kB per hour. 每小时保存的基本数据很小,约为50kB。 However, the results (including spectrum and waterfall plots) could be >2MB per hour (this is a program that one might run for many hours a day). 但是,结果(包括频谱图和瀑布图)可能每小时超过2MB(此程序一天可能运行多个小时)。 To analyze "on the go" and just throw the results in a scrolling buffer I know would be very fast, but I want my program to allow the user to look back at specific time segments in the past. 要分析“行进中”并将结果仅放入滚动缓冲区中,我知道这会非常快,但是我希望我的程序允许用户回顾过去的特定时间段。 The question I have is should the model object analyze the data and store the results alongside the basic data, or should the model only store the basic data, and return that data to the controller which would then analyze it to present it to the view (this would be very CPU heavy if regraphing even minutes of data, let alone hours)? 我的问题是模型对象应该分析数据并将结果与​​基本数据一起存储,还是模型仅存储基本数据,然后将数据返回给控制器,然后由控制器进行分析以将其呈现给视图(如果重新绘制甚至数分钟的数据(更不用说数小时),这将占用大量CPU资源?

Any thoughts or suggestions would be greatly appreciated, as I feel laying proper groundwork could save me untold hours of coding (and fixed/debugging) later. 任何想法或建议都将不胜感激,因为我认为打下适当的基础可以为我节省许多小时的编码(和固定/调试)时间。

As for your question 1: 至于你的问题1:

I suggest you to write a class/object which manages the bluetooth data, separately from the app delegate. 我建议您编写一个与应用程序委托分开的类/对象来管理蓝牙数据。 The app delegate is where the view objects meet the controller, and as such there will be lots of calls to AppKit (on OS X) and to UIKit (on iOS). 应用程序委托是视图对象与控制器相遇的地方,因此将有很多调用AppKit(在OS X上)和UIKit(在iOS上)。 The change will be so great that #ifdef between the OSes inside the same file won't make much sense for the app delegate. 更改将非常巨大,以至于同一文件内的操作系统之间的#ifdef对于应用程序委托而言并没有多大意义。

Rather, make an ivar holding the Bluetooth controller inside the app delegate. 相反,创建一个将蓝牙控制器保持在应用程序委托中的ivar。 That way your code will be better structured, and will be easier to be reused. 这样,您的代码将具有更好的结构,并且将更易于重用。

As for your question 2: 至于你的问题2:

On an OS X machine, which usually comes with plenty of RAM these days, holding and caching all the resulting data on the RAM would be just fine, if it's 2MB per hour. 在如今通常配备大量RAM的OS X计算机上,将所有结果数据保存并缓存在RAM上就可以了,如果每小时2MB。

On an iOS machine, RAM is a seriously endangered resource. 在iOS机器上,RAM是严重濒危的资源。 If your program caches the calculated data in the memory and consumes a lot of RAM and the user send it to the background, the OS might outright kill your program instead of suspending it, for example. 例如,如果您的程序将计算出的数据缓存在内存中并消耗大量RAM,并且用户将其发送到后台,则操作系统可能会彻底杀死您的程序,而不是挂起它。 Then you'll need to recalculate data anyway, because your app is re-launched. 然后,无论如何,您都需要重新计算数据,因为您的应用程序已重新启动。

The filesystem capacity is quite big even on an iOS machine. 即使在iOS计算机上,文件系统容量也很大。 So one way out is to write out your calculated data onto the disk, and let the view controller reload the previous calculated data from there. 因此,一种解决方法是将计算出的数据写到磁盘上,然后让视图控制器从那里重新加载以前的计算出的数据。 That way, your program can access pre-calculated data even after it's relaunched. 这样,即使重新启动程序,您的程序也可以访问预先计算的数据。

That cacheing code can be even shared between OS X and iOS, if you don't hard-code the cache directory into the program. 如果您不将缓存目录硬编码到程序中,则甚至可以在OS X和iOS之间共享该缓存代码。

If your software on the iPhone is supposed to run continuously in the background processing data from BTstack, I recommend to create a LaunchDaemon for the data processing and provide a regular app for the configuration. 如果您的iPhone上的软件应在BTstack的后台处理数据中连续运行,我建议创建一个LaunchDaemon进行数据处理并为配置提供常规应用程序。 (Although BTstack Mouse / Keyboard / GPS don't follow this advice, they will when I get around to update them - Celeste uses a daemon for the actual file transfers eg) (尽管BTstack鼠标/键盘/ GPS不遵循此建议,但当我四处更新它们时它们会-Celeste使用守护程序进行实际文件传输,例如)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM