1

我有一个 user 表和一个 Girls 表和一个 Girls_result 表。

基本上我只想要USER实体中的一个用户,那个 USER 是设备所有者,我想在每次需要时调用同一个 USER。

USER可以有多个女孩,女孩可以有多个结果。
数据模型: USER <------>>GIRLS<-------->>GIRLS_RESULTS

因此,我将 user_deviceid 属性添加到 USER Entity 并创建了一个单例类,这样我就可以为 user 设置一个唯一编号,并在需要时调用它。我想确保没有创建具有相同 UUID 的多个用户对象。

@implementation SingletonClass

@synthesize singletonManagedObjectContext=_singletonManagedObjectContext;
@synthesize userIS=_userIS;

+ (SingletonClass *)sharedInstance
{
    static SingletonClass *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[SingletonClass alloc] init];
        // Do any other initialisation stuff here
    });
    return sharedInstance;
}

- (id)init {
    if (self = [super init]) {

        // set a singleton managed object context
        _singletonManagedObjectContext=self.singletonManagedObjectContext;

        //set only one user based on unique device id, so all tables will belong to that user
        NSManagedObjectContext *context = _singletonManagedObjectContext;

        //how to make sure that _userIS is the same user all the time???

        if (_userIS.user_deviceid == [self newUUID]) {
            NSLog(@"user dvice id matches");
        }
        else{
            _userIS.user_deviceid=[self newUUID];
        }


    }
    return self;
}

- (NSString *)newUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return (NSString *)CFBridgingRelease(string);
} 

那么如何确保在 USER 实体中只创建一个 USER 并且每次都可以使用该特定 USER 呢?

============编辑答案============ 基于接受的答案 下面我创建了一个工作正常的类

#import "CheckUser.h"
#import "USER.h"
#import "SingletonClass.h"

@implementation CheckUser


- (USER *)checkandreturnUser
{
    SingletonClass *sharedInstance = [SingletonClass sharedInstance];
    NSManagedObjectContext *context = sharedInstance.singletonManagedObjectContext;
    // Fetch Form Object
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"USER"
                                        inManagedObjectContext:context]];
    USER *user;
    NSError *error = nil;
    NSArray *userArray = [context executeFetchRequest:fetchRequest error:&error];
    NSLog(@"user array count %i",[userArray count]);
    if (userArray == nil) {
        NSAssert(0, @"There was an error fetching user object");
        userArray = [NSArray array];
    }
    // If user doesn't exist create it
    if ([userArray count] > 0) {
        NSAssert([userArray count] == 1, @"Expected one user but there were more");
        user = [userArray objectAtIndex:0];
    } else {
        //Create user object
        user = [NSEntityDescription insertNewObjectForEntityForName:@"USER"
                                             inManagedObjectContext:context];
        user.user_deviceid=[self newUUID];
        // maybe save here?
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }


    return user;
}
- (NSString *)newUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return (NSString *)CFBridgingRelease(string);
}
4

3 回答 3

8

Core Data 不支持单例实体的想法。如果您只想在数据存储中拥有一个实体实例,则需要编写应用程序以确保您永远不会创建多个。

原因很简单,这是一个奇怪的命题,不符合数据存储的概念。如果您只有一个实例,则不需要为该实例使用 Core Data。将您的单例数据保存在用户默认值或自定义文件中。把它放在数据存储中没有多大意义。你不需要它和其他对象之间的关系,因为你总是知道这些关系是什么——它们是不可配置的,它们不需要被查找,所以它们不需要存在。

于 2013-01-25T17:16:38.350 回答
1

最简单的解决方案可能是检查并确保您创建的用户不超过一个。一旦你有了一个用户实体,就停下来。

[self newUUID]总是会返回一个不同的值所以_userIS.user_deviceid == [self newUUID]总是NO

我会使用访问器_userIS

- (NSManagedObject *)user
{
  if (_userIS) {
    return _userIS;
  }

  NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
  [fetchRequest setEntity:[NSEntityDescription entityForName:@"user" 
                                      inManagedObjectContext:_singletonManagedObjectContext]];
  NSError *error = nil;
  NSArray *userArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
  if (userArray == nil) {
    NSAssert(0, @"There was an error fetching user object");
    userArray = [NSArray array];
  }
  // If user doesn't exist create it
  if ([userArray count] > 0) { 
    NSAssert([userArray count] == 1, @"Expected one user but there were more");
    _userIS = [userArray objectAtIndex:0];
  } else {
    //Create user object
    _userIS = [NSEntityDescription insertNewObjectForEntityForName:@"user" 
                                                 inManagedObjectContext:_singletonManagedObjectContext];
    _userIS.user_deviceid=[self newUUID];
    // maybe save here?
  }
  [_userIS retain]; // Non-ARC only
  return _userIS;
}

还值得一提的是,拥有一个根对象并不少见,它是模型-视图-控制器-存储模式的一部分。

于 2013-01-25T17:19:50.577 回答
0

你应该覆盖- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context,而不是init。假设您的模型对象名称是用户,它看起来像这样

- (id)          initWithEntity:(NSEntityDescription *)entity 
insertIntoManagedObjectContext:(NSManagedObjectContext *)context
    static User * _Singleton = nil;
    if (!_Singleton)
    {
        self = [super initWithEntity:entity 
      insertIntoManagedObjectContext:context];
    }
    return self;
于 2013-01-25T17:25:20.497 回答