Search code examples
iosobjective-ccore-datarestkitnsfetchedresultscontroller

CoreData + Restkit What task to perform when the user is signing out from the app?


Could somebody explain me what tasks should I perform while the user is signing out from the app which include rest kit framework synchronised with core data?

In my app I synchronised the rest kit together with core data by doing the following steps:

1 I created core data model with entities and relationships between them.

2 I created DateModel class where I created shareDataModel singleton to setup core data from appDelegate class.

+ (id)sharedDataModel {
    static DateModel *__sharedDataModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __sharedDataModel = [[DateModel alloc] init];
    });

    return __sharedDataModel;
}

- (NSManagedObjectModel *)managedObjectModel {
    return [NSManagedObjectModel mergedModelFromBundles:nil];
}

- (void)setup
{  
    NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"AppModel" ofType:@"momd"]];
    NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];

    self.objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[self managedObjectModel]];

    NSError *error;

    _objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
    [_objectStore createPersistentStoreCoordinator];

    NSPersistentStore __unused *persistentStore = [_objectStore addInMemoryPersistentStore:&error];
    NSAssert(persistentStore, @"Failed to add persistent store: %@", error);

    [self.objectStore createManagedObjectContexts];

    [RKManagedObjectStore setDefaultStore:self.objectStore];

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:kBase_URL]];
    objectManager.managedObjectStore =_objectStore;

    [RKObjectManager setSharedManager:objectManager];
}

3 I created MappingProvider class where I mapped every database table from the server to RKEntityMapping objects like in example below.

+(RKMapping *)userWearersMapping
{
    RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"UserWearers" inManagedObjectStore:[[DateModel sharedDataModel]objectStore]];

    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"id": @"userWearer_id",
                                                  @"created_at":@"created_at",
                                                  @"updated_at":@"updated_at",
                                                  @"user_id":@"user_id",
                                                  @"wearer_id":@"wearer_id"
                                                  }
     ];
    [mapping setIdentificationAttributes:@[@"userWearer_id"]];

    return mapping;
}

4 In AppDelegate method: -didFinishLaunchingWithOptions, I've setup coredata sharedModel by using previously created singleton in DateModel class.

[[DateModel sharedDataModel] setup];

In my app I'm displaying data from the server in UITableViewControllers by using NSFetchedResultsController. I followed the bellows steps:

5 In MainViewControler I added NSFetchRequestController and NSManagedObjectContext properties

6 Then in -viewDidLoad method I initialised the NSManagedObjectContext from my DateModel class

_managedObjectContext = [[DateModel sharedDataModel]objectStore].mainQueueManagedObjectContext;

7 I implemented NSFetchResultsControllerDelegate methods:

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:...
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:...
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
- (NSFetchedResultsController *)fetchedResultsController

Everything works great till the moment when I change the user. When new user is signing in to the app the UITableViewController displaying data for both current and previous logged in user.

What should I do in this case? What changes should I make when the user is signing out from my app?


Solution

  • Get the current user, using a fetch request or a relationship (or a property that already stores it), and delete that user from your model. The delete rules should be configured to cascade the deletion to everything that the user 'owns' so that it is also purged from the system.

    If you wanted to you could retain all of the current user data and instead use a predicate on your fetched results controller to filter the contents to the 'new' current user. Be careful here as when users sign out they may expect their data to be purged so that it couldn't be accessed by anyone else...