【发布时间】:2014-05-26 13:34:23
【问题描述】:
在我的 iPhone 应用程序中,我正在与 Rails 上的 Web 应用程序通信,该应用程序从特定数据库表中接收 json 文件。我有三个表用户、用户佩戴者和佩戴者,关系如下:
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
只有 Users 和 Wearers 表接收我可以映射到我的核心数据模型中的 json 文件。 Userwearer 表包含如下行:user_id、wearer_id、created_at、updated_at,并且没有收到任何 json 响应。我完全是映射对象的业余爱好者,所以如果我问你一些愚蠢的问题,请理解:) 如果我理解正确,这是一种多对多的关系我在我的应用程序中所做的是 UserAuthentication 并在 UITableViewController 中显示佩戴者并配置 NSFetchResultsController 委托方法.一切正常,直到我尝试注销用户并使用其他凭据登录。在这种情况下,UITableViewController 会显示前一个用户和当前用户的数据。在我看来,我应该只向现有的 fetchedResultsController 委托方法 NSPredicate 添加有关仅显示来自当前用户的数据的信息,并且一旦用户退出只是为了从具有相关实体的核心数据中删除当前用户。我对吗?
所以知道我必须映射这 3 个表之间的关系。如果没有来自 UserWearers 表的信息,有什么方法可以做到这一点?如何正确映射它们之间的关系或者也许有其他方法可以解决我的问题?
+(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;
}
【问题讨论】:
-
是的,您要删除该用户。不清楚你的问题实际上是什么。您显示映射代码,但您的问题似乎是关于注销时的删除,我看不到任何代码。请澄清...
-
因为我不确定我是否可以在没有来自表 UserWearers 的 json 的情况下映射这些表之间的关系?然后我会创建像 NSPredicate *predicate =[NSPredicate predicateWithFormat:@"userwearers.user.user_id == %@", currentUser.user_id ]; 这样的谓词
-
目前我只映射了users和wearers之类的表,没有userWearers。我不确定是否可以在没有 userWearers 和 Wearer 之间的映射关系连接的情况下删除当前用户信息
-
当用户退出后,你可以删除 Core Data 存储中的所有内容吗?
-
也许可以,但我不知道该怎么做 :) 我应该选择哪种方法从核心数据存储中删除所有内容?
标签: ios ruby-on-rails json core-data restkit