RestKit 一对多关系在映射 JSON 响应后没有正确数量的对象
RestKit one-to-many relationship doesn't have the correct number of objects after mapping JSON response
我有一个 NSManagedObject
子类 TAGRemoteObject
TAGRemoteObject.h
extern const struct TAGRemoteObjectAttributes {
__unsafe_unretained NSString *object_Id;
} TAGRemoteObjectAttributes;
@interface TAGRemoteObject ()
@property (nonatomic, strong) NSString *object_Id;
@end
TAGRemoteObject.m
const struct TAGRemoteObjectAttributes TAGRemoteObjectAttributes = {
.object_Id = @"object_Id",
};
@implementation TAGRemoteObject
@dynamic object_Id;
+(RKEntityMapping*)entityMappingInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
{
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
@"object_id" : TAGRemoteObjectAttributes.object_Id,
}];
[entityMapping setIdentificationAttributes:@[ TAGRemoteObjectAttributes.object_Id ]];
return entityMapping;
}
@end
然后我有两个TAGRemoteObject
的子类。 TAGPost
与 TAGUser
具有一对多 viewedUsers
关系。 TAGUser
与 TAGPost
.
也有一对多的 posts
关系
TAGPost.h
extern const struct TAGPostRelationships {
__unsafe_unretained NSString *viewedUsers;
} TAGPostRelationships;
@interface TAGPost : TAGRemoteObject ()
@property (nonatomic, strong) NSSet *viewedUsers;
@end
TAGPost.m
const struct TAGPostRelationships TAGPostRelationships = {
.viewedUsers = @"viewedUsers",
};
@implementation TAGPost
@dynamic viewedUsers;
+(RKEntityMapping*)entityMappingInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
{
RKEntityMapping *entityMapping = [super entityMappingInManagedObjectStore:managedObjectStore];
RKEntityMapping *userMapping = [TAGUser entityMappingInManagedObjectStore:managedObjectStore];
RKRelationshipMapping *viewedUsersMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"viewed_users" toKeyPath:TAGJourneyOutgoingPostRelationships.viewedUsers withMapping:userMapping];
viewedUsersMapping.assignmentPolicy = RKUnionAssignmentPolicy;
[entityMapping addPropertyMapping:viewedUsersMapping];
return entityMapping;
}
@end
TAGUser.h
extern const struct TAGUserAttributes {
__unsafe_unretained NSString *username;
} TAGUserAttributes;
extern const struct TAGUserRelationships {
__unsafe_unretained NSString *posts;
}
@interface TAGUser : TAGRemoteObject ()
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSSet *posts;
@end
TAGUser.m
const struct TAGUserAttributes TAGUserAttributes = {
.username = @"username",
};
const struct TAGUserRelationships TAGUserRelationships = {
.posts = @"posts",
};
@implementation TAGUser
@dynamic username;
@dynamic posts;
+(RKEntityMapping*)entityMappingInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
{
RKEntityMapping *entityMapping = [super entityMappingInManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
@"username" : TAGUserAttributes.username,
}];
RKEntityMapping *postEntityMapping = [TAGPost entityMappingInManagedObjectStore:managedObjectStore];
RKRelationshipMapping *postsRelationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"posts" toKeyPath:TAGUserRelationships.posts withMapping:postEntityMapping];
postsRelationshipMapping.assignmentPolicy = RKUnionAssignmentPolicy;
[entityMapping addPropertyMapping:postsRelationshipMapping];
return entityMapping;
}
@end
我遇到的问题是,当我收到这样的 JSON 响应(针对有很多用户的主页)时,我得到的 viewedUsers.count
为 3,这符合预期:
{
"error": null,
"result": [
{
"object_id": 1,
"username": "a",
"posts": [
{
"object_id": 299,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
},
{
"object_id": 5,
"username": "b"
}
]
}
]
}
]
}
但是当我为用户 1
发送所有 post 的请求并得到以下响应时,viewedUsers.count
对每个 [=86= 随机为 0 或 1 ]:
{
"error": null,
"result": [
{
"object_id": 299,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
},
{
"object_id": 5,
"username": "b"
}
]
},
{
"object_id": 300,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
}
]
},
{
"object_id": 301,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
},
{
"object_id": 5,
"username": "b"
}
]
}
]
}
当我向 homefeed 发送请求时,我映射到 [TAGUser entityMappingInManagedObjectStore:self.objectManager.managedObjectStore];
当我发送请求为用户获取 post 时,我映射到 [TAGPost entityMappingInManagedObjectStore:self.objectManager.managedObjectStore];
我真的不知道为什么映射有时不起作用。当我将 RestKit 的日志记录级别设置为 RKLogLevelTrace
时,它看到长度为 3 的 JSON 数组,但是映射后,关系只有 0 或 1。
编辑: 我怀疑循环关系可能是问题所在,所以我从 TAGUser
映射中删除了 posts
关系。不过我仍然看到这个问题。
原来关系被设置为一对多,而它本应设置为多对多。
我有一个 NSManagedObject
子类 TAGRemoteObject
TAGRemoteObject.h
extern const struct TAGRemoteObjectAttributes {
__unsafe_unretained NSString *object_Id;
} TAGRemoteObjectAttributes;
@interface TAGRemoteObject ()
@property (nonatomic, strong) NSString *object_Id;
@end
TAGRemoteObject.m
const struct TAGRemoteObjectAttributes TAGRemoteObjectAttributes = {
.object_Id = @"object_Id",
};
@implementation TAGRemoteObject
@dynamic object_Id;
+(RKEntityMapping*)entityMappingInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
{
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
@"object_id" : TAGRemoteObjectAttributes.object_Id,
}];
[entityMapping setIdentificationAttributes:@[ TAGRemoteObjectAttributes.object_Id ]];
return entityMapping;
}
@end
然后我有两个TAGRemoteObject
的子类。 TAGPost
与 TAGUser
具有一对多 viewedUsers
关系。 TAGUser
与 TAGPost
.
posts
关系
TAGPost.h
extern const struct TAGPostRelationships {
__unsafe_unretained NSString *viewedUsers;
} TAGPostRelationships;
@interface TAGPost : TAGRemoteObject ()
@property (nonatomic, strong) NSSet *viewedUsers;
@end
TAGPost.m
const struct TAGPostRelationships TAGPostRelationships = {
.viewedUsers = @"viewedUsers",
};
@implementation TAGPost
@dynamic viewedUsers;
+(RKEntityMapping*)entityMappingInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
{
RKEntityMapping *entityMapping = [super entityMappingInManagedObjectStore:managedObjectStore];
RKEntityMapping *userMapping = [TAGUser entityMappingInManagedObjectStore:managedObjectStore];
RKRelationshipMapping *viewedUsersMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"viewed_users" toKeyPath:TAGJourneyOutgoingPostRelationships.viewedUsers withMapping:userMapping];
viewedUsersMapping.assignmentPolicy = RKUnionAssignmentPolicy;
[entityMapping addPropertyMapping:viewedUsersMapping];
return entityMapping;
}
@end
TAGUser.h
extern const struct TAGUserAttributes {
__unsafe_unretained NSString *username;
} TAGUserAttributes;
extern const struct TAGUserRelationships {
__unsafe_unretained NSString *posts;
}
@interface TAGUser : TAGRemoteObject ()
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSSet *posts;
@end
TAGUser.m
const struct TAGUserAttributes TAGUserAttributes = {
.username = @"username",
};
const struct TAGUserRelationships TAGUserRelationships = {
.posts = @"posts",
};
@implementation TAGUser
@dynamic username;
@dynamic posts;
+(RKEntityMapping*)entityMappingInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
{
RKEntityMapping *entityMapping = [super entityMappingInManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
@"username" : TAGUserAttributes.username,
}];
RKEntityMapping *postEntityMapping = [TAGPost entityMappingInManagedObjectStore:managedObjectStore];
RKRelationshipMapping *postsRelationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"posts" toKeyPath:TAGUserRelationships.posts withMapping:postEntityMapping];
postsRelationshipMapping.assignmentPolicy = RKUnionAssignmentPolicy;
[entityMapping addPropertyMapping:postsRelationshipMapping];
return entityMapping;
}
@end
我遇到的问题是,当我收到这样的 JSON 响应(针对有很多用户的主页)时,我得到的 viewedUsers.count
为 3,这符合预期:
{
"error": null,
"result": [
{
"object_id": 1,
"username": "a",
"posts": [
{
"object_id": 299,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
},
{
"object_id": 5,
"username": "b"
}
]
}
]
}
]
}
但是当我为用户 1
发送所有 post 的请求并得到以下响应时,viewedUsers.count
对每个 [=86= 随机为 0 或 1 ]:
{
"error": null,
"result": [
{
"object_id": 299,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
},
{
"object_id": 5,
"username": "b"
}
]
},
{
"object_id": 300,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
}
]
},
{
"object_id": 301,
"viewed_users": [
{
"object_id": 3,
"username": "b"
},
{
"object_id": 4,
"username": "b"
},
{
"object_id": 5,
"username": "b"
}
]
}
]
}
当我向 homefeed 发送请求时,我映射到 [TAGUser entityMappingInManagedObjectStore:self.objectManager.managedObjectStore];
当我发送请求为用户获取 post 时,我映射到 [TAGPost entityMappingInManagedObjectStore:self.objectManager.managedObjectStore];
我真的不知道为什么映射有时不起作用。当我将 RestKit 的日志记录级别设置为 RKLogLevelTrace
时,它看到长度为 3 的 JSON 数组,但是映射后,关系只有 0 或 1。
编辑: 我怀疑循环关系可能是问题所在,所以我从 TAGUser
映射中删除了 posts
关系。不过我仍然看到这个问题。
原来关系被设置为一对多,而它本应设置为多对多。