robbiehanson / XMPPFramework

An XMPP Framework in Objective-C for Mac and iOS
Other
5.91k stars 2.09k forks source link

Clean Database when Logout. #543

Open koolahmedkhan opened 9 years ago

koolahmedkhan commented 9 years ago

Hi Geeks ,

I am having an issue cleaning up the database after user logout. When new user login from the same device the messages of old user been displayed.

Please help me out :(

ObjColumnist commented 9 years ago

Typical you save data with the XMPP Stream JID of the current user, so you need to include that in your predicate.

itsaboutcode commented 9 years ago

I cleaned up the roster data using following code.

-(void) logout {
[self deleteAllEntities:@"XMPPGroupCoreDataStorageObject"
              inContext:managedObjectContext_roster];
[self deleteAllEntities:@"XMPPResourceCoreDataStorageObject"
              inContext managedObjectContext_roster];
[self deleteAllEntities:@"XMPPUserCoreDataStorageObject"
              inContext: managedObjectContext_roster];
}
 -(void)deleteAllEntities:(NSString *)nameEntity inContext:(NSManagedObjectContext*)theContext {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:nameEntity];
[fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSError *error;
NSArray *fetchedObjects = [theContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *object in fetchedObjects)
{
    [theContext deleteObject:object];
}

error = nil;
[theContext save:&error];
}