我有一个用户表和一个女孩表和一个girls_result表。
基本上我只想在USER
实体中有一个用户,USER是设备所有者,我想在每次需要时调用同一个USER。
用户可以有多个女孩,女孩可以有多个结果
数据模型:
USER <------>>GIRLS<-------->>GIRLS_RESULTS
所以我将user_deviceid属性添加到USER Entity并创建了一个单例类,这样我就可以为用户设置一个唯一的编号,并在需要时调用它。我想确保没有创建具有相同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);
}
答案 0 :(得分:6)
核心数据不支持单身实体的想法。如果您想在数据存储中只有一个实体实例,则需要编写应用程序以确保永远不会创建多个实体。
原因很简单,这是一个奇怪的命题,不符合数据存储的想法。如果您只有一个实例,则不需要为该实例使用Core Data。将单例数据保存在用户默认值或自定义文件中。将它放在数据存储中并没有多大意义。你不需要它与其他对象之间的关系,因为你总是知道这些关系是什么 - 它们是不可配置的,它们不需要被查找,因此它们不需要存在。
答案 1 :(得分:1)
最简单的解决方案是检查并确保不创建多个USER。一旦你有一个用户实体就停止。
[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;
}
还值得一提的是,拥有根对象并不罕见,并且是模型 - 视图 - 控制器 - 存储模式的一部分。
答案 2 :(得分:0)
您应该覆盖- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
,而不是init
。假设您的模型对象名称是User,它看起来像这样
- (id) initWithEntity:(NSEntityDescription *)entity
insertIntoManagedObjectContext:(NSManagedObjectContext *)context
static User * _Singleton = nil;
if (!_Singleton)
{
self = [super initWithEntity:entity
insertIntoManagedObjectContext:context];
}
return self;