具有核心数据和大量业务逻辑的 Cocoa 应用程序布局
对于那些看过我的其他问题的人:我正在取得进展,但我还没有把注意力集中在这方面。我一直在研究 stackoverflow 的答案和像 Cocoa With Love 这样的网站,但我还没有找到合适的应用程序布局(为什么如此缺乏科学或商业应用程序示例?食谱和书籍示例太简单了)。
我有一个数据分析应用程序,其布局如下:
通信管理器(单例,管理硬件)
DataController(告诉 Comm.mgr 做什么,并检查它收到的原始数据)
模型(从数据控制器接收数据,清理、分析和存储)
MainViewController(现在的骨架,监听 comm.mgr 来呈现视图和警报)
现在,我的数据永远不会直接显示在视图上(就像一个简单的实体和属性表),我可能会使用核心图来绘制分析结果(一旦我弄清楚)。保存的原始数据将是巨大的(10,000 个点),我使用包装在 ObjC++ 类中的 C++ 向量来访问它。矢量类还具有encodeWithCoder和initWithCoder函数,它们使用NSData作为矢量的传输。我试图遵循正确的设计实践,但我不知道如何将持久存储放入我的应用程序中(存储和查看旧数据集需要它)。
我读过一些资料,说“业务逻辑”应该进入模型类。这就是我现在的方式,我向它发送原始数据,它解析、清理和分析结果,然后将它们保存到 ivar 数组(向量类)中。但是,我还没有看到具有托管对象的核心数据示例,该对象不仅仅是非常基本属性(字符串、日期)的简单存储,而且它们从来没有任何业务逻辑。所以我想知道如何融合这两个方面?我的所有分析都应该进入数据控制器并让它管理对象上下文吗?如果是这样,我的模型在哪里? (如果我的数据存储在我的控制器中,似乎会破坏 MVC 架构 - 阅读:因为这些是向量数组,所以我不能不断地将它们编码和解码为 NSData 流,在将它们保存到磁盘之前它们需要一个存在的地方与核心数据,在我从存储中检索它们并解码它们以供审查后,它们需要一个存在的地方)。
任何建议都会有帮助(即使是在我已经开始的布局上)。我只是画了一些对象之间的通信来给你一个想法。另外,我在模型和视图/视图控制器之间还没有任何连接(目前使用 NSLog)。
For those who have seen my other questions: I am making progress but I haven't yet wrapped my head around this aspect. I've been pouring over stackoverflow answers and sites like Cocoa With Love but I haven't found an app layout that fits (why such a lack of scientific or business app examples? recipe and book examples are too simplistic).
I have a data analysis app that is laid out like this:
Communication Manager (singleton, manages the hardware)
DataController (tells Comm.mgr what to do, and checks raw data it receives)
Model (receives data from datacontroller, cleans, analyzes and stores it)
MainViewController (skeleton right now, listens to comm.mgr to present views and alerts)
Now, never will my data be directly shown on a view (like a simple table of entities and attributes), I'll probably use core plot to plot the analyzed results (once I figure that out). The raw data saved will be huge (10,000's of points), and I am using a c++ vector wrapped in an ObjC++ class to access it. The vector class also has the encodeWithCoder and initWithCoder functions which use NSData as a transport for the vector. I'm trying to follow proper design practices, but I'm lost on how to get persistent storage into my app (which will be needed to store and review old data sets).
I've read several sources that say the "business logic" should go into the model class. This is how I have it right now, I send it the raw data, and it parses, cleans and analyzes the results and then saves those into ivar arrays (of the vector class). However, I haven't seen a Core Data example yet that has a Managed Object that is anything but a simple storage of very basic attributes (strings, dates) and they never have any business logic. So I wonder, how can I meld these two aspects? Should all of my analysis go into the data controller and have it manage the object context? If so, where is my model? (seems to break the MVC architecture if my data is stored in my controller - read: since these are vector arrays, I can't be constantly encoding and decoding them into NSData streams, they need a place to exist before I save them to disk with Core Data, and they need a place to exist after I retrieve them from storage and decode them for review).
Any suggestions would be helpful (even on the layout I've already started). I just drew some of the communication between objects to give you an idea. Also, I don't have any of the connections between the model and view/view controllers yet (using NSLog for now).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而向量>>非常适合处理您正在采样的数据(因为它支持动态调整底层存储大小),您可能会发现直接 C 数组足以(甚至更好)处理已存储的数据。这确实增加了一定程度的复杂性,但它避免了对已经具有已知静态大小的数据数组进行复制。
NSData 的 -bytes 返回一个指向 NSData 对象内的原始数据的指针。 Core Data 支持 NSData 作为其属性类型之一。如果你知道数据中每一项的大小,那么你可以使用 -length 来计算元素的数量等。
在采样方面,我建议使用向量<>当您收集数据并间歇性地将数据复制到 NSData 属性并保存时。注意:我在这种方法中遇到了一些问题(截断的核心数据 NSData 对象 )我归因于 Core Data 无法识别当 NSData 属性由 NSMutableData 对象支持并且该可变对象的数据发生更改时对 NSData 属性所做的更改。
至于MVC问题。我建议数据(模型)由模型管理。视图和控制器可以向模型请求数据(或数据子集)以便显示。但数据的所有权属于模型。就我而言,可能与您的情况类似,有时模型会返回删节的数据集(使用 Douglas-Peucker 算法)。视图和控制器对于点被丢弃一无所知——尽管他们对模型的请求可能在其中发挥了作用(图形缩放因子等)。
更新
这是我的 Data 类中的一段代码,它扩展了 NSManagedObject。对于文件系统解决方案,NSFileHandle 的 -writeData: 和用于监视文件偏移的方法可能允许类似(更好)的管理控制。
While vector<> is great for handling your data that you are sampling (because of its support for dynamically resizing underlying storage), you may find that straight C arrays are sufficient (even better) for data that is already stored. This does add a level of complexity but it avoids a copy for data arrays that are already of a known and static size.
NSData's -bytes returns a pointer to the raw data within an NSData object. Core Data supports NSData as one its attribute types. If you know the size of each item in data, then you can use -length to calculate the number of elements, etc.
On the sampling side, I would suggest using vector<> as you collect data and, intermittently, copy data to an NSData attribute and save. Note: I ran into a bit of problem with this approach (Truncated Core Data NSData objects) that I attribute to Core Data not recognizing changes made to NSData attribute when it is backed by an NSMutableData object and that mutable object's data is changed.
As for MVC question. I would suggest that data (model) is managed in by Model. Views and Controllers can ask Model for data (or subsets of data) in order to display. But ownership of data is with the Model. In my case, which may be similar to yours, there were times when the Model returns abridged data sets (using Douglas-Peucker algorithm). The views and controllers were none the wiser that points were being dropped - even though their requests to the Model may have played in a role in that (graph scaling factors, etc.).
Update
Here is a snippet of code from my Data class which extends NSManagedObject. For a filesystem solution, NSFileHandle's -writeData: and methods for monitoring file offset might allow similar (better) management controls.
我认为您可能对核心数据的解读有点太多了。我对此没有那么丰富的经验,所以我以非专家的身份发言,但数据存储系统基本上有两类。
首先是基本数据库,例如 SQLite、PostgreSQL 或任何数量的解决方案。它们的目的是存储和检索数据;没有逻辑,所以你必须弄清楚如何管理表等。有时有点棘手,但它们非常有效。他们最擅长管理大量原始数据;如果您想要包含该数据的对象,您必须自己创建和管理它们。
然后你就有像 Core Data 这样的东西,它不应该被视为数据库,而应该被视为“对象持久性”框架。使用 Core Data,您可以创建对象、存储它们,然后将它们作为对象检索。它非常适合拥有大量对象的应用程序,每个对象都包含多条数据以及与其他对象的关系。
根据我对您情况的有限了解,我大胆猜测一个真正的数据库可能更适合您的需求,但您必须在那里做出决定(就像我说的,我对您的情况不太了解) 。
对于MVC,我的观点是视图应该只包含显示代码,模型应该只包含管理数据本身及其存储的代码,控制器应该包含处理数据的逻辑。在您的情况下,听起来您正在收集原始数据并在存储之前对其进行处理,在这种情况下,您希望有另一个对象在将数据存储在模型中之前处理数据,然后是一个单独的控制器来排序、管理、并以其他方式在视图接收数据之前准备数据。再次强调,这可能不是最好的解释(或者适合您情况的最佳方法),因此请持保留态度。
编辑:另外,如果您想更多地了解核心数据,我喜欢这本书。它比我更好地解释了整个对象持久性与数据库的概念。
I think you may be reading into Core Data a bit too much. I'm not that experienced with it, so I speak as a non-expert, but there are basically two categories of data storage systems.
First is the basic database, like SQLite, PostgreSQL, or any number of solutions. These are meant to store data and retrieve it; there's no logic so you have to figure out how to manage the tables, etc. It's a bit tricky at times but they're very efficient. They're best at managing lots of data in raw form; if you want objects with that data you have to create and manage them yourself.
Then you have something like Core Data, which shouldn't be considered a database as much as an "object persistence" framework. With Core Data, you create objects, store them, and then retrieve them as objects. It's great for applications where you have large numbers of objects that each contain several pieces of data and relationships with other objects.
From my limited knowledge of your situation, I would venture a guess that a true database may be better suited to your needs, but you'll have to make the decision there (like I said, I don't know much about your situation).
As for MVC, my view is that the view should only contain display code, the model should only contain code for managing the data itself and its storage, and the controller should contain the logic that processes the data. In your case it sounds like you're gathering raw data and processing it before storing it, in which case you'd want to have another object to process the data before storing it in the model, then a separate controller to sort, manage, and otherwise prepare the data before the view receives it. Again, this may not be the best explanation (or the best methods for your situation) so take it with a grain of salt.
EDIT: Also, if you're looking on getting into Core Data more, I like this book. It explains the whole object-persistence-vs-database concept a lot better than I can.