CoreData中的单例实体

dub*_*eat 6 iphone cocoa-touch core-data

使用单词single可能会在标题中使用我的术语.我现在正在寻找一种好的技术.我有一个名为user的实体,用于存储登录数据的用户,例如用于发出服务器请求的会话密钥.我只想要其中一个实体存在.这样做有标准的技术吗?

到目前为止我所拥有的是这样的

NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription
                                              entityForName:@"UserEntity" inManagedObjectContext:moc];
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setEntity:entityDescription];


NSArray *array = [moc executeFetchRequest:request error:&error];
    if (array == nil)
    {
        // Deal with error...
    }

    if ([array count]==0) {
         //first run of app

    }else if([array count]==1)
    {
        // id like the code to enter here after every app run except for the first one

    }else
    {

        //dont want this to happen
    }
Run Code Online (Sandbox Code Playgroud)

Max*_*eod 3

我使用 Matt Gallagher 在他的文章Singletons、AppDelegates 和顶级数据中描述的方法中描述的方法。

它使用宏创建一个“合成单例”类,然后您可以从任何地方访问该类。对于会话、托管对象上下文等非常方便。否则您将不得不在各处传递这些信息。