如何在 cocoa (iPhone) 中创建用于核心数据访问的数据工厂的示例?

发布于 2024-09-09 02:13:31 字数 1586 浏览 2 评论 0原文

我一直在慢慢地学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执妄 2024-09-16 02:13:31

按照命名约定,您的 fetchAllDrivers 应返回一个 autoreleased 对象。

- (NSMutableArray*)fetchAllDrivers
{

    if (!self.appDelegate) {
        // Big Problems Raise exception immediately if you want...
        return 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 = nil;

    NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[context executeFetchRequest:request error:&error] copyItems:YES];

    if (error) {
        // Something went wrong
        [results release];
        // Error handling code here
        [request release];
        return nil;
    }

    [request release];
    return [results autorelease];

}

Following naming conventions, your fetchAllDrivers should return an autoreleased object.

- (NSMutableArray*)fetchAllDrivers
{

    if (!self.appDelegate) {
        // Big Problems Raise exception immediately if you want...
        return 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 = nil;

    NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[context executeFetchRequest:request error:&error] copyItems:YES];

    if (error) {
        // Something went wrong
        [results release];
        // Error handling code here
        [request release];
        return nil;
    }

    [request release];
    return [results autorelease];

}
小嗲 2024-09-16 02:13:31
NSMutableArray* arr = [dataFactory fetchAllDrivers];
[arr retain];
//Some code  where we use arr
[arr release];

按照惯例,从外部对象的方法返回的任何对象都是自动释放的。除了属性之外,您不需要保留它们。如果您仅在方法的本地范围内使用 arr ,则不需要保留/释放它。它是自动释放的,并且会在本地作用域结束后消亡。

如果您需要让 arr 挂在对象内部。您应该将其存储在保留属性中:

@property (nonatomic,retain) NSMutableArray *arr;

... 然后将其与 self 表示法一起使用以确保保留:

self.arr=[dataFactory fetchAllDrivers];

... 然后您只需在类的 dealloc 方法中释放它。

让一个对象管理您的数据模型是一个非常好的主意,但它不是一个“工厂”。 Objective-c 不使用像 C++ 和类似语言那样的工厂。试图用这些方式思考将会导致悲伤。相反,该对象应被视为“控制器”或“管理器”。

NSMutableArray* arr = [dataFactory fetchAllDrivers];
[arr retain];
//Some code  where we use arr
[arr release];

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:

@property (nonatomic,retain) NSMutableArray *arr;

... then use it with the self notation to ensure retention:

self.arr=[dataFactory fetchAllDrivers];

... 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文