当尝试用对象向服务器发送DELETE请求时,如果您像这样填充success
块,RestKit将尝试引用一个已释放的对象:
[[APIClient objectManager] deleteObject:object path:path parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
success();
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
failure([error localizedDescription]);
}];
success()
回调目前没有任何作用,因为我注意到RestKit会自动为我们删除本地Core Data对象
如果我将success
块设置为nil
,那么世界上一切都很好,但我宁愿有办法知道它是成功的。
我猜它试图在映射结果中引用已删除的对象,但我不能确定。服务器只返回一个no content
头,因此没有任何映射。
是不是我做错了什么导致了这一切?
为了安全起见,请将代码重写为:
[[APIClient objectManager] deleteObject:object path:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (success != nil) {
success();
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if (failure != nil) {
failure([error localizedDescription]);
}
}];
因为尝试执行nil
块是一件坏事。
此外,在定义success
和failure
的属性时,请确保将它们设置为copy
(而不是strong
)。