views:

131

answers:

2

Hello,

I have an app which is a UITabBarController, I have defined two subviews

Both tabs have their Class attribute in the Identity Inspector set to UINavigationController.

Now i have managed to get this far with my coding after VERY LONG trials.

- (void)viewDidLoad {
[super viewDidLoad];

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = appDelegate.managedObjectContext;

{



  NSError *error = nil;
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  [fetchRequest setEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext]];
  NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

  NSEntityDescription *entityDesc =
  [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext];

  // replace the old data with new. this DOESNT WORK
  if (fetchedItems.count > 0)
  {
   Usr *newUsr;
   for (newUsr in fetchedItems)
   {
    if ([newUsr.name isEqualToString:@"Line One"])
    {

     newUsr.uName = @"Line One (new)";

    }
   }
  }

  //add a new default data. THIS ADDS DATA TO MY TABLEVIEW BUT IT DOESNT SAVE THEM TO THE SQLITE
  User *addedDefaultdata = nil;
  addedDefaultdata = [[User alloc] initWithEntity:entityDesc insertIntoManagedObjectContext:self.managedObjectContext];
  addedDefaultdata.name = @"Added new 1";
  [addedDefaultdata release];
 }


NSError *error = nil;
if (![User.managedObjectContext save:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

}

and my appdelegate looks like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

 [application setStatusBarStyle:UIStatusBarStyleBlackOpaque];


 [window addSubview:navigationController.view];
 [window makeKeyAndVisible];

}

now I cannot quire the "User" at all! although i get no errors or warnings!

Any suggestions would be much appreciated!

Thanks

A: 

It seems that you may be asking how to update to CoreData?

If so, you need to use the save: method on NSManagedObjectContext, like this:

NSError *error;
[managedObjectContent save:&error];
if (error) {
    ...
}
Paul Lynch
yes i want to update my coredata app. but i dont understand why i get a nil managedobjectcontext and therefor no "save or update? it has something to do with the managedobjctcontext cause i ve read somewhere that this is wrong: myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]; self.managedObjectContext = appDelegate.managedObjectContext; but it s driving me crazy now... UPDATE: i dont know how the NSError *error;... line got there...it s NOT on my original code...(i cant even copy-paste) LOLHowever the question stands the same!Any help?
treasure
If you created the app from one of the standard templates, then your app delegate will have a valid MOC that you can use. You don't say in your original question that your MOC is nil. Is it? You still need to call save:, whichever.
Paul Lynch
yes it does have a valid MOC.however i get this error when i run the code (updated above):...Classes/UsrRootViewController.m:109:0 error: expected ':' before '.' tokenany ideas?
treasure
That's a syntax error you should correct before attempting to run.
Paul Lynch
NSError *error = nil; if (![User.managedObjectContext save: exit(-1); } i cant find the syntax error in the above.
treasure
I'd guess that moc isn't a property on the User class (it's capitalised, so it's a class, right?).
Paul Lynch
it is a model class!
treasure
A: 

FIXED IT

thank you all for your suggestions and answers!

treasure