我的UserFavourite类看起来像这样:
UserFavourite.h
#import <Foundation/Foundation.h>
#import <Parse/Parse.h>
#import "Product.h"
@interface UserFavourite : PFObject <PFSubclassing>
@property (nonatomic, copy) Product* product;
@property (nonatomic, copy) PFUser* user;
+(NSString *)parseClassName;
-(id)initWithProducts:(Product*)product
user:(PFUser *)user;
@end
UserFavourite.m
#import "UserFavourite.h"
#import <Parse/PFObject+Subclass.h>
@implementation UserFavourite
@dynamic product, user;
+(NSString *)parseClassName {
return @"UserFavourite";
}
-(id)initWithProducts:(Product*)product
user:(PFUser *)user{
self = [super init];
if (self) {
self.product = product;
self.user = user;
}
return self;
}
@end
这是我插入的代码:
//Getting product from Parse
PFQuery *query = [PFQuery queryWithClassName:@"Product"];
UserFavourite *uf = [[UserFavourite alloc] init];
[query getObjectInBackgroundWithId:@"NhKrnlzeeN" block:^(PFObject *product1, NSError *error) {
[uf setUser:[PFUser currentUser]];
Product* pr = [[Product alloc] init];
pr.name = [product1 objectForKey:@"name"];
pr.category = [product1 objectForKey:@"category"];
pr.price = [product1 objectForKey:@"price"];
pr.ingridients = [product1 objectForKey:@"ingridients"];
pr.protein = [product1 objectForKey:@"protein"];
pr.fats = [[product1 objectForKey:@"fats"] integerValue];
pr.carbo = [[product1 objectForKey:@"carbo"] integerValue];
pr.calories = [[product1 objectForKey:@"calories"] integerValue];
pr.likes = [[product1 objectForKey:@"likes"] integerValue];
pr.quantity = [[product1 objectForKey:@"quantity"] integerValue];
pr.status = [[product1 objectForKey:@"status"] integerValue];
pr.objectId = product1.objectId;
[uf setProduct:pr];
NSLog(@"uf %@", uf);
}];
[uf saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
// The object has been saved.
} else {
NSLog(@"Error: %@", error.description);
}
}];
我想将此保存到Parse服务器,但是这些属性不会保存到表中,如图所示: 我也在AppDelegate中初始化了这个子类。 我在这做错了什么? 谢谢。
答案 0 :(得分:0)
对getObjectInBackgroundWithId
的调用是异步方法调用。以下对saveInBackgroundWithBlock
的调用将在前一个调用完成之前调用,在进行任何修改之前保存uf
。
要解决此问题,请将保存移至上面的完成块。