RT 创建AVObject子类, objectID 为空

下面是我的子类

.h

#import <AVOSCloud.h>
#import "User.h"

@interface Comment : AVObject<AVSubclassing>
@property (strong,nonatomic) User *commentUser; //评论用户
@property (copy,nonatomic) NSString* commentContent; //评论内容
@property (strong,nonatomic) User *replayToUser; //关联用户

+(instancetype)commentWithCommentUser:(User*)u commentContent:(NSString*)content replayTo:(User*)replayToUser;
@end

.m

#import "Comment.h"

@implementation Comment
@dynamic commentUser,commentContent,replayToUser;

+(instancetype)commentWithCommentUser:(User*)u commentContent:(NSString*)content replayTo:(User*)replayToUser{
//    Comment *c=[[Comment alloc]initWithClassName:NSStringFromClass([Comment class])];
//    Comment *c=[Comment objectWithClassName:NSStringFromClass([Comment class])];
    Comment *c=[Comment object];
    c.commentUser=u;
    c.commentContent=content;
    c.replayToUser=replayToUser;
    return c;
}

+(void)load{
    [Comment registerSubclass];
}

+(NSString *)parseClassName{
    return NSStringFromClass([Comment class]);
}
@end

然后我用 +(instancetype)commentWithCommentUser:(User*)u commentContent:(NSString*)content replayTo:(User*)replayToUser; 这个方法创建 类的时候,返回的 内容如下:

Printing description of c:
<Comment, Comment, (null), localData:{
    commentContent = "\U6c5f\U6e56\U4eba\U79f0\U5c04\U51fb\U6e38\U620f";
    commentUser = "<User, _User, 56e58132b01460002906c309, localData:{\n    \"__type\" = Object;\n    authData = \"<null>\";\n    avatarPath = \"http://ac-RD1BgVPw.clouddn.com/AsASjfQemOt5KNytj8MKQ9D\";\n    displayName = \"2@qq.com\";\n    emailVerified = 0;\n}, estimatedData:{\n}, relationData:{\n    friends =     (\n        \"<User, _User, (null), localData:{\\n}, estimatedData:{\\n}, relationData:{\\n}>\"\n    );\n}>";
}, estimatedData:{
}, relationData:{
}>

明显 (null) 是 objectID ,为什么是 null 呢..愁 .. frowning

P.S. 维护社区的 Coder 们辛苦了.. ~

都没人帮我解决下问题... frowning

我自己解决了...


下面4种创建对象的方式产生的结果是一样的..显然第一种最方便:

  • [Comment object];
  • [Comment objectWithClassName:NSStringFromClass([Comment class])];
  • [[Comment alloc]initWithClassName:NSStringFromClass([Comment class])];
  • [[Comment alloc]init];

然后创建出来的对象都是没有 ObjectID 的..要调用 save, saveInBackGroundXXX 之类的方法保存到服务器上才有 ObjectID,

举一个栗子 🌰

AVUser 的子类 ,User 对象有一个 @property (strong,nonatomic) AVFile *avatarFile; 保存用户头像的图片

正确的方式

    User *u=[User user];
   
    NSString *avatarUrlStr=nil;
    AVFile *f=[AVFile fileWithURL:avatarUrlStr];
    [f saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        u.avatarFile=f;
        [u saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            //接下来的逻辑处理 ~
        }];
    }];

错误的方式

    User *u=[User user];
    
    NSString *avatarUrlStr=nil;
    AVFile *f=[AVFile fileWithURL:avatarUrlStr];
    u.avatarFile=f;
    [u saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        //接下来的逻辑处理 ~
    }];

要先保存 AVFile,让其存在于服务器上,再和 User 对象关联才能 🆗

文档中 https://leancloud.cn/docs/leanstorage_guide-ios.html#构建对象2

构建对象

构建一个 AVObject 可以使用如下方式:


    // objectWithClassName 参数对应控制台中的 Class Name
    AVObject *todo = [AVObject objectWithClassName:@"Todo"];

    // 或调用实例方法创建一个对象
    AVObject *todo = [[AVObject alloc] initWithClassName:@"Todo"];

    // 以上两行代码完全等价

每个 objectId 必须有一个 Class 类名称,这样云端才知道它的数据归属于哪张数据表。

是不是更新下文档 , [AVObject object]; 感觉这样会更好些..

希望 Leancloud 一直能为开发者提供稳定便捷的服务 smile