如何在 cocoa (iPhone) 中创建用于核心数据访问的数据工厂的示例?
我一直在慢慢地学习iPhone开发,似乎一直在碰壁,我不知道如何以正确的方式做我想做的事:(
基本上,我想要一个处理与数据层的所有交互的类,例如,从数据存储中获取一些对象列表的可变数组,
这在具有垃圾收集器的其他语言中非常简单,但在 iPhone 上的 Objective-C 中,我不知道该怎么做
。请注意我们不确定何时发布的注释。
- (NSMutableArray*)fetchAllDrivers{
NSMutableArray *results = [[NSMutableArray alloc] init];;
if (self.appDelegate != nil) {
NSManagedObjectContext *context = [self.appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
[request setEntity: entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
[request setSortDescriptors: sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error;
results = [[context executeFetchRequest:request error:&error] mutableCopy];
if (results == nil) {
//something went wrong
}
//Where should this be released??? Certainly not here!
[results release];
[request release];
}
else {
[NSException raise:@"Can't fetch b/c app delgate is nil!" format: @"!!!"];
}
return results;
}
调用代码,与我的注释相关:
NSMutableArray* arr = [dataFactory fetchAllDrivers];
[arr retain];
//Some code where we use arr
[arr release];
I have been slowly learning iPhone development and seem to keep hitting walls where I can't figure out how to do what I want to do the right way :(
Basically, I want a class that handles all interactions with the data layer, for example, getting a mutable array of some list of objects from the data store.
This is pretty trivial in other languages where you have a garbage collector, but in Objective-C on the iPhone, I'm not sure what to do.
This is an example method on a DataFactory class we were creating. Note the comment on where we are not sure when to release....
- (NSMutableArray*)fetchAllDrivers{
NSMutableArray *results = [[NSMutableArray alloc] init];;
if (self.appDelegate != nil) {
NSManagedObjectContext *context = [self.appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
[request setEntity: entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
[request setSortDescriptors: sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error;
results = [[context executeFetchRequest:request error:&error] mutableCopy];
if (results == nil) {
//something went wrong
}
//Where should this be released??? Certainly not here!
[results release];
[request release];
}
else {
[NSException raise:@"Can't fetch b/c app delgate is nil!" format: @"!!!"];
}
return results;
}
Calling code, related to my comment:
NSMutableArray* arr = [dataFactory fetchAllDrivers];
[arr retain];
//Some code where we use arr
[arr release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照命名约定,您的
fetchAllDrivers
应返回一个autoreleased
对象。Following naming conventions, your
fetchAllDrivers
should return anautoreleased
object.按照惯例,从外部对象的方法返回的任何对象都是自动释放的。除了属性之外,您不需要保留它们。如果您仅在方法的本地范围内使用 arr ,则不需要保留/释放它。它是自动释放的,并且会在本地作用域结束后消亡。
如果您需要让
arr
挂在对象内部。您应该将其存储在保留属性中:... 然后将其与 self 表示法一起使用以确保保留:
... 然后您只需在类的
dealloc
方法中释放它。让一个对象管理您的数据模型是一个非常好的主意,但它不是一个“工厂”。 Objective-c 不使用像 C++ 和类似语言那样的工厂。试图用这些方式思考将会导致悲伤。相反,该对象应被视为“控制器”或“管理器”。
By convention, any object returned from the method of an external object is autoreleased. You don't need to retain them except in properties. If you only using
arr
in the local scope of the method then you don't need to retain/release it. It is autoreleased and will die after the end of the local scope.If you need to have
arr
hang around inside the object. You should store it in a retained property:... then use it with the self notation to ensure retention:
... then you need only release it in the class'
dealloc
method.Having one object manage your data model is very good idea but it is not a "factory". Objective-c does not use factories like C++ and similar languages. Trying to think in those terms will lead to grief. The object should instead be thought of as a "controller" or "manager".