核心数据+ RestKit映射多对多关系?

时间:2014-05-26 13:34:23

标签: ios ruby-on-rails json core-data restkit

在我的iPhone应用程序中,我正在与在特定数据库表中接收json文件的rails上的Web应用程序进行通信。我有三个表Users,UserWearers和Wearers,其关系如下:

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

只有用户和佩戴者表接收我可以映射到我的核心数据模型的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;
}

1 个答案:

答案 0 :(得分:0)

删除所有内容的最简单方法是删除sqlite文件并销毁(nil)对您的应用当前使用的所有托管对象,上下文和持久存储的所有引用。然后再次运行初始化代码以重新创建核心数据堆栈(将创建一个新的空的sqlite文件)。

以上是非常快速和有效的。另一种方法是获取所有对象并删除它们,如果你没有太多的对象,这是一个可行的选择。