Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:15:03+00:00 2026-06-18T08:15:03+00:00

I have a user table and a girls table and a girls_result table. Basically

  • 0

I have a user table and a girls table and a girls_result table.

Basically I want only one user in USER entity, that USER is the device owner, and I want to call that same USER every time I need it.

USER can have multiple girls, and girls can have multiple results.
Data Model:
USER <------>>GIRLS<-------->>GIRLS_RESULTS

So I added user_deviceid attribute to USER Entity and created a singleton class so I can set a unique number for the user , and call it when I need it. I want to make sure multiple user objects with same UUID is not created.

@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);
} 

So How can I make sure that only one USER is created in USER entity and I can use that specific USER each time?

============EDIT ANSWER============
Based On Accepted Answer Below I have created a class it works fine

#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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:15:04+00:00Added an answer on June 18, 2026 at 8:15 am

    The easiest solution is to probably check and make sure that you don’t create more than one USER. Once you have one user entity just stop.

    [self newUUID] will always return a different value so _userIS.user_deviceid == [self newUUID] would always be NO

    I would use an accessor for _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;
    }
    

    Its also worth mentioning that having a root object is not uncommon and is part or the Model-View-Controller-Store pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user table that contain 8 records. I want to arrange the
I have one user table. and second language table which contains language that user
I have a User table that has serialized data in one of the tables.
I have a User table that has all of their avatars saved in an
I have a user table 'users' that has fields like: id first_name last_name ...
Currently I have a User table that has a toys_owned column that is an
I have an user defined table function in SQL Server that aggregate data from
I have a User table that has a BIT NULL column called NxEnabled ,
I have a user table and a user_detail table with one to one mapping
I have a user table. I want to insert data into my user table.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.