PFUser 子类化:仅本地(非数据库)用户属性



我已经对PFUser进行了子类化,添加了几个直接转换为数据库存储的属性,例如性别和出生日期:

// Define property
@property(retain, nonatomic) NSDate *birthDate; 
// and later on...
// Instruct compiler to leave setter/getter up to us (Parse)
@dynamic signUpComplete, birthDate, paypalEmail, gender;

这些属性工作正常,并且可靠地提交到存储。

我的问题:如何添加存储在数据库中的常规"私有"属性?

我最初的想法是像往常一样简单地定义常规属性,并调用@syntheize而不是@dynamic。这似乎工作正常,但 Parse 确实向控制台转储了一条关于未找到 PFUser 密钥的消息。

@synthensize是否会可靠地阻止仅本地属性写入数据库?

我会覆盖PFUser子类中的saveInBackground:方法以实现您的目标。像这样:

头文件:

@interface PFUserSubclass : PFUser
@property (nonatomic, strong) NSString *customPropertyToSave;
@property (nonatomic, strong) NSString *customPropertyToIgnore;
- (void)saveInBackgroundWithBlock:(PFBooleanResultBlock)block;
@end

实现文件:

@implementation PFUserSubclass
@synthesize customPropertyToSave;
@synthesize customPropertyToIgnore;
- (void)saveInBackgroundWithBlock:(PFBooleanResultBlock)block {
    PFUser *userToSave = [PFUser currentUser];
    [userToSave setObject:self.customPropertyToSave forKey:@"customProperty"];
    [userToSave saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        block(succeeded, error);
    }];
}
@end

最新更新