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 6337089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:11:09+00:00 2026-05-24T19:11:09+00:00

Inside my user object I have the following code to generate a new ‘session’

  • 0

Inside my user object I have the following code to generate a new ‘session’ or continue the existing session if one exists.

Strangely it will keep other properties but just loses the ‘user’ property… user is in a one to many relationship with session, 1 user can have many sessions. (or will do, for the following test I am simply checking for any previous session and using it if it exists)

-(void)setupSessionStuff
{
// Create new Core Data request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];

// Create Sort Descriptors for request
NSSortDescriptor *startTimeSort = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:NO selector:nil];
[request setSortDescriptors:[NSArray arrayWithObjects:startTimeSort, nil]];
[startTimeSort release];

[request setFetchLimit:1]; // Only get the most recent session

// Execute request
NSError *error = nil;
NSArray *results = [[self managedObjectContext] executeFetchRequest:request error:&error];
if (results == nil) {
    // Something went horribly wrong...
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);
}
[request release];

Session *theSession = nil;

if ([results count] == 1) {
    NSLog(@"existing session");
    // Use existing Session
    theSession = [results objectAtIndex:0];

    NSLog(@"session.user: %@", [theSession valueForKey:@"user"]); // this is always null!

} else {
    NSLog(@"new session");
    // Create new Sesson
    theSession = (Session *)[NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:[self managedObjectContext]];

    // Add the Session to the User
    NSLog(@"before: session.user: %@", theSession.user); // null
    theSession.user = self;
    NSLog(@"after: session.user: %@", theSession.user); // looks good
}

...


NSLog(@"before.save: session.user: %@", theSession.user); // good

// Save everything
error = nil;
if (![[self managedObjectContext] save:&error]) {
    // Something went horribly wrong...
    NSLog(@"Unresolved error: %@, %@, %@", error, [error userInfo],[error localizedDescription]);
    exit(-1);
}

NSLog(@"after.save: session.user: %@", theSession.user); // still there..
}

Additionally I have opened up the Core Data sqlite file and examined with SQLite Manager. It looks like the relationship has been correctly saved, as I can see the userID stored in the session table.

–

Just added this at the start of my method as another test.

NSSet *set = self.session;

for(Session *sess in set) {
    NSLog(@"startTime %@", sess.startTime);
    NSLog(@"user %@", sess.user);

}

Strangely enough the user is set in this case!? So set here then not set a few lines later when I do the fetch request… ?

–

In response to feedback below

Have added this code after assigning session.user = self and both return the expected output. So it does look like the problem is with the subsequent fetch.

NSLog(@"self.session: %@", self.session);
NSLog(@"self.session: %@", [self valueForKey:@"session"]);

Also I agree that accessing my session’s through self.session will let me work around my issue, but it doesn’t solve what is going on here.
In other places I surely won’t be able to walk from one entity to the other so need to confidence the fetch is going to pull everything in correctly.

  • 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-05-24T19:11:10+00:00Added an answer on May 24, 2026 at 7:11 pm

    Well I found the problem and solved my issue…

    After examining the memory address of my session entity I noticing that it was changing between runs. Investigating further I discovered that where I had been testing some code earlier in another class, creating a new session entity, but not saving it, well, it was being saved after all – when I issued the save in my code above!

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

Sidebar

Related Questions

I have a couple of update panels inside a user control. One of the
I have the following code: <?php if (!$this->Auth->user()) { echo $this->element('header'); } else {
I have gridview binded from database.. I have following code: protected void Page_Load(object sender,
I have a user control nested in a repeater. Inside my user control I
I have a GridView inside of a User Control populated from a List of
I have a web user control with a dropdownlist inside of it. When the
I have created a site collection inside a web application with user A as
I have a button and image inside it in my silverlight user control. When
I have following code, but validation for the custom type( UserDetails ) are not
I have the following domain object: public class DomainObject<T,TRepo> where T : DomainObject<T> where

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.