ユーザーテーブルと女の子テーブルとgirls_resultテーブルがあります。
基本的に、エンティティに1人のユーザーのみが必要でUSER
あり、そのUSERはデバイスの所有者であり、必要になるたびに同じUSERを呼び出します。
USERは複数の女の子を持つことができ、女の子は複数の結果を持つことができます。
データ・モデル:
USER <------>>GIRLS<-------->>GIRLS_RESULTS
そこで、user_deviceid属性を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エンティティに1つの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);
}