Ios 核心数据+RestKit映射多对多关系?
在我的iPhone应用程序中,我与rails上的web应用程序通信,该应用程序从特定的数据库表接收json文件。我有三张表,分别是用户、用户佩戴者和佩戴者,其关系如下:Ios 核心数据+RestKit映射多对多关系?,ios,ruby-on-rails,json,core-data,restkit,Ios,Ruby On Rails,Json,Core Data,Restkit,在我的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:
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
删除所有内容的最简单方法是删除sqlite文件,并销毁对应用程序当前使用的所有托管对象、上下文和持久存储的所有引用。然后再次运行初始化代码以重新创建核心数据堆栈,这将创建一个新的空sqlite文件
以上是非常快速和有效的。另一种选择是获取所有对象并删除它们,如果没有太多对象,这是一种可行的选择。是,您希望删除用户。不清楚你的问题到底是什么。您显示了映射代码,但您的问题似乎是关于注销时的删除,我看不到相关代码。请澄清…因为我不确定如果没有来自表用户的json,我是否可以映射这些表之间的关系?然后我会创建一个谓词,比如NSPredicate*谓词=[NSPredicate predicateWithFormat:@userwears.user.user\u id==%@,currentUser.user\u id];目前我只有像users和wearers这样的映射表,没有userWearers。我不确定我是否可以删除当前用户信息,而不需要用户穿戴者和穿戴者之间的映射关系连接。当用户注销时,你可以删除核心数据存储中的所有内容吗?也许我可以,但我不知道如何做:我应该选择哪种方法删除核心数据存储中的所有内容?还有一个问题,我应该把这行代码放到注销按钮功能中吗?[[[RKObjectManager sharedManager]managedObjectStore]resetPersistentStores:&错误];是的,这会起作用-但是如果你选择这条路线,你需要确保当你重置时没有正在进行的下载,你将有问题。谢谢你的回答,我仍然在与rest工具包和核心数据作斗争:如果你有时间,请看一下我的下一个问题:
+(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;
}