/** 通过Entry查找是否收藏 */
- (void)fetchCollectWithEntry:(EntryModel *)entry
type:(NSUInteger)type
success:(successBlock)block{
if (type == add) {
AVObject *collect = [[AVObject alloc] initWithClassName:@"Collection"];
[collect setObject:entry forKey:@"entry"];
[collect setObject:[AVUser currentUser] forKey:@"user"];
[collect saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// 添加收藏成功
block(YES);
}else{
// 添加收藏失败
block(NO);
}
}];
return;
}
AVQuery *query = [[AVQuery alloc] initWithClassName:@"Collection"];
[query whereKey:@"entry" equalTo:entry];
[query whereKey:@"user" equalTo:[AVUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count > 0) {
switch (type) {
case find:{
// 确认本篇文章收藏成功
block(YES);
break;
}
case delete:{
// 确认本篇文章收藏成功
[objects.firstObject deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
// 删除成功
block(YES);
}else{
// 删除失败
block(NO);
}
}];
break;
}
default:{
// 未知错误
block(NO);
break;
}
}
}else{
NSLog(@"未查找到收藏");
block(NO);
}
}else{
NSLog(@"查找收藏出现错误---%@---",error);
block(NO);
}
}];