0
votes

In my iPhone app I'm communicating with the web application on rails that receive json files from the specific database tables. I have a three tables Users, UserWearers and Wearers with the relationships like below:

class User < ActiveRecord::Base
has_secure_password
has_many :user wearers
has_many :wearers, :through => :user wearers
end
class Userwearer < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :user
belongs_to :wearer
end
class Wearer < ActiveRecord::Base
has_many :user wearers
has_many :users, :through => :user wearers
end

Only Users and Wearers tables receive json files that I can map into my Core Data model. The Userwearer table contains rows like: user_id, wearer_id, created_at, updated_at, and doesn't receive any json response. Im totally amateur of mapping objects so please be understanding if I ask you some stupid question :) If I understand correctly it is kind of many to many relationship What Ive done in my app is the UserAuthentication and displaying wearers in UITableViewController with configured NSFetchResultsController delegate methods. Everything working ok till the moment when I'm trying to sign out the user and log in with the other credentials. In that case the UITableViewController display data of the previous and the current user. It seems to me that I should just add to the existing fetchedResultsController delegate method NSPredicate with info about displaying only data from the current user, and once the user is signing out just to delete current user from the core data with the related entities. Am I right?

So know I have to map relationships between these 3 tables. Is there any way to do this without information from the table UserWearers? How can I properly map relationships between them Or maybe there is some other way to fix my issue?

+(RKMapping *)usersMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Users" inManagedObjectStore:[[EdisseDateModel sharedDataModel]objectStore]];

[mapping addAttributeMappingsFromDictionary:@{
                                              @"id": @"user_id",
                                              @"address1": @"address1",
                                              @"created_at":@"created_at",
                                              @"updated_at": @"updated_at",
                                              @"email": @"email",
                                              @"name":@"name",
                                              @"password_digest": @"password_digest",
                                              @"phone_no": @"phone_no",
                                              @"postcode":@"postcode",
                                              @"remember_token":@"remember_token",
                                              @"user_type": @"user_type",
                                            }
 ];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"userwearer" toKeyPath:@"userWearers" withMapping:[MappingProvider userWearersMapping]]];


[mapping setIdentificationAttributes:@[@"user_id"]];
return mapping;
}
/**
Return Mapping for entity named : UserWearers
 @return RKEntityMapping object
*/
+(RKMapping *)userWearersMapping{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"UserWearers" inManagedObjectStore:[[EdisseDateModel sharedDataModel]objectStore]];

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

[mapping addConnectionForRelationship:@"wearer" connectedBy:@{@"wearer_id": @"wearer_id"}];
[mapping addConnectionForRelationship:@"user" connectedBy:@{@"user_id": @"user_id"}];
[mapping setIdentificationAttributes:@[@"userWearer_id"]];
return mapping;
}
/**
 Return Mapping for entity named : Wearers
@return RKEntityMapping object
*/
+ (RKEntityMapping *)wearersMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Wearers" inManagedObjectStore:[[EdisseDateModel sharedDataModel] objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
                                              @"id":@"wearer_id",
                                              @"at_risk": @"at_risk",
                                              @"created_at": @"created_at",
                                              @"dob": @"dob",
                                              @"status":@"status",
                                              @"updated_at":@"updated_at",
                                              }
];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"userWearers" toKeyPath:@"userWearers" withMapping:[MappingProvider userWearersMapping]]];

[mapping setIdentificationAttributes:@[@"wearer_id"]];
return mapping;
}
1
Yes, you want to delete the user. Not clear what your question actually is though. You show mapping code, but your question seems to be about deletion on logout and I see no code for that. Please clarify... - Wain
Because Im not sure if I can map relationships between these tables without json from table UserWearers? Then I would just create predicate like NSPredicate *predicate =[NSPredicate predicateWithFormat:@"userwearers.user.user_id == %@", currentUser.user_id ]; - sonoDamiano
Currently I have only mapped tables like users and wearers without userWearers. Im not sure if I can delete current user info without mapped relationship connection between userWearers and Wearer - sonoDamiano
When the user is logged out can you just delete everything in the Core Data store? - Wain
Maybe I can, but I have no idea how to do this :) Which method should I choose to delete everything from the core data store? - sonoDamiano

1 Answers

0
votes

The easiest way to delete everything is to delete the sqlite file and to destroy (nil) all references to all of the managed objects, contexts and persistent stores that your app is currently using. Then run your initialisation code again to recreate the core data stack (which will create a new, empty, sqlite file).

The above is very fast and efficient. The alternative is to fetch all objects and delete them, which, if you don't have too many objects is a viable alternative.