对所有型号的iVar使用一个设置器 [英] Using one setter for all model iVars
问题描述
我有一系列适用于我的应用程序的模型.在所有这些模型中,有(将有)大约200或300个实例变量.该应用程序将其持久性数据存储在基于Web的服务器上(MySQL-但我想那部分并不重要).每当更新模型iVar时,我都需要致电服务器以更新该iVar的适当值.
I have a series of models for my application. Across all these models there are (will be) some 200 or 300 instance variables. The application stores its persistent data on a web-based server (MySQL - but I guess that part doesn't matter). Whenever a model iVar is updated I need to make a call to the server to update the appropriate value for that iVar.
我当前的模型策略是(头文件):
My current model strategy is (header file):
@interface MyModel : NSObject {
NSString * firstName;
NSString * lastName;
}
@property (readwrite, copy) NSString * firstName;
@property (readwrite, copy) NSString * lastName;
@end
(实现文件):
@implementation MyModel
@synthesize firstName;
@synthesize lastName;
-(id)init {
[super init]
[self setFirstName:@"George"];
[self setLastName:@"Kastanza"];
return self;
}
-(void)setFirstName:(NSString *)aName {
// call method to update server with new value here
firstName = aName;
}
-(void)setLastName:(NSString *)aName {
// call method to update server with new value here
lastName = aName;
}
@end
问题是,如果我有200或300个iVar,都需要对服务器进行相同的更新调用,这意味着编写 lot 的二传手.而且,如果需要更改方法调用,则必须更新整个应用程序中每个setter中的每个方法.
The problem is that if I have 200 or 300 iVar's all needing to go through the same update call to the server that means writing a lot of setters. Moreover, if I need to make a change to the method call, I'd have to update each and every method in every setter i the entire application.
在设置之前,有没有一种方法可以首先通过一种方法运行每组iVar?
Is there a process by which I could run every set of an iVar through a method first, before setting?
我想到每个模型对象只有一个NSMutableDictionary
来存储所有iVar,但这抽象了setter和getter,并可能为许多词典带来很大的内存占用.但是,以这种方式进行操作意味着每次设置字典后,我都可以通过一种方法来传递它.
I thought of having just a NSMutableDictionary
per model object to store all of the iVar's, but that abstracts the setters and getters and may introduce a big memory footprint for so many dictionaries. However, doing it this way means that every time the dictionary is set I could pass it through one method.
据我所知,在运行时将iVar的动态添加到对象模型被认为是一件坏事,因为指针引用了可能依赖于模型的任何子类(除非完全重新编译子类指针,否则子类指针不会偏移)完成).
As I understand it dynamically adding iVar's at runtime to an object model is considered a bad thing because of the pointer referencing for any subclasses that may be dependent upon the model (the subclass pointer doesn't get offset unless a complete recompile is done).
非常感谢任何想法和建议.
Any ideas and suggestions much appreciated.
更新
基于Ole的建议,这里是解决方案(尽管不幸的是,它使用的代码比几行要多)...
Based upon Ole's recommendation here is the solution (although it uses a little more code than a few lines unfortunately)...
在模型中,我添加了一个可以在需要时设置的方法.我没有直接从init调用该方法,因为添加从服务器返回的一堆结果会触发添加的每个对象的观察者.因此,在初始化并更新了服务器上的第一个抓取之后,我将方法称为 .
In the model I added a method that I can set when I need to. I didn't call the method directly from the init, because adding a whole bunch of results returned from the server would trigger the observers for every object added. So I call the method after I have initialized and updated the first grab from the server.
这是代码...
-(void)registerObservers {
[self addObserver:self
forKeyPath:@"firstName"
options:NSKeyValueObservingOptionNew
context:NULL];
[self addObserver:self
forKeyPath:@"lastName"
options:NSKeyValueObservingOptionNew
context:NULL];
}
然后将观察者添加到模型中:
Then I add the observer to the model:
-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqual:@"firstName"]) {
// Do whatever I need to do
}
if ([keyPath isEqual:@"lastName"]) {
// Do whatever I need to do
}
}
在我的实际实现中,我也碰巧发布了一个对象设置为self
的通知,以便我可以更新任何应该监听但没有引起注意的内容(例如NSArrayControllers
中的内容).
In my real implementation I also happen to post a notification of the object set to self
so that I can update anything that should be listening but isn't paying attention (like stuff in NSArrayControllers
).
推荐答案
使用键值观察.不过,您必须手动将自己注册为每个属性的观察者.
Use Key-Value Observing. You have to manually register yourself as an observer for every property, though.
这篇关于对所有型号的iVar使用一个设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!