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

  • Home
  • SEARCH
  • 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 6138529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:52:46+00:00 2026-05-23T17:52:46+00:00

I need to delete all user info and recreate it. The code i use

  • 0

I need to delete all user info and recreate it. The code i use to create everything is in the app delegate:

- (NSManagedObjectContext *)managedObjectContext {
    if (managedObjectContext != nil) {
        return managedObjectContext;
    }
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return managedObjectContext;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
    NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES) lastObject];
    NSURL *storeURL = [NSURL fileURLWithPath: [dir stringByAppendingPathComponent:
                                           @"Data.sqlite"]];
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                               configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    return persistentStoreCoordinator;
}

Then, i delete everything with:

[context reset];

NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES) lastObject];
NSURL *storeURL = [NSURL fileURLWithPath: [dir stringByAppendingPathComponent:
                                           @"Data.sqlite"]];

NSError *error;
NSURL *storeURL = store.URL;
NSPersistentStoreCoordinator *storeCoordinator = [((CustomAppDelegate *)[[UIApplication sharedApplication] delegate]) persistentStoreCoordinator];

NSPersistentStore *store = [[storeCoordinator persistentStores] objectAtIndex:0];

[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

[storeCoordinator release];
storeCoordinator = nil;

And it seems to work, the problem is when i try to save the new data, afterwards. If i do something like:

NSManagedObjectContext *afterContext = [appDelegate managedObjectContext];
NSEntityDescription *userEntity = [NSEntityDescription entityForName:@"User"
                                              inManagedObjectContext:afterContext];
NSManagedObject *user = [NSEntityDescription
                         insertNewObjectForEntityForName:[userEntity name] inManagedObjectContext:afterContext];
[user setValue:@"John" forKey:@"firstName"];
if(![afterContext save:&error])
    NSLog(@"oops");

it crashes in afterContext save, it gaves me a EXC_BAD_INSTUCTION exception. When i do
[appDelegate managedObjectContext] it creates the new persistentStore, so i don’t know what could be the problem. A reference to a previous object? The stacktrace does not give any clue at all.

  • 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-23T17:52:47+00:00Added an answer on May 23, 2026 at 5:52 pm

    When i do [appDelegate
    managedObjectContext] it creates the
    new persistentStore

    That is correct but only if one does not already exist. You can not “take” an object that you do not own, release it and then set it to nil and expect it to work. The line where you are setting storeCoordinator to the app delegates coordinator is just setting the pointer value. When you release that coordinator at the end and set it to nil the AppDelegate is still set to the address of the now deallocated coordinator. That is the reason for your crash. You will need to come up with a way of releasing and nil’ing the value in the actual AppDelegate itself. Also the managed object context in the appDelegate will need to updated to reflect the changes.

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

Sidebar

Related Questions

I need to create Trigger for delete action which backsup all fields old value
I'm trying to create a script that will delete all user properties for a
Using Python I need to delete all characters in a multiline string up to
Hey, I need to delete all images from a string and I just can't
my goals (this is for training purpose): User can create(50%DONE)/edit/delete/view a created Quiz! The
I am the administrator and I need to delete a user. If the user
In need to move/delete files on the local file system of the user from
Please help. I use tinyMCE as inline editor. So I need that when user
Are primitives worth keeping? Should all the deprecated stuff be deleted? Do we need
I need to delete a temporary file from my C++ windows application (developed in

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.