首页 > 代码库 > AVOS Cloud 学习笔记(二) 功能总结(What it can do?)
AVOS Cloud 学习笔记(二) 功能总结(What it can do?)
AVOS Cloud 学习笔记(一) 功能总结(What it can do?)
第一章、对象存储
- 对象新建、保存、更新、删除和检索
1 //创建新对象,根据类名2 AVObject *gameScore = [AVObject objectWithClassName:@"GameScore"];3 [gameScore setObject:[NSNumber numberWithInt:1337] forKey:@"score"];4 [gameScore setObject:@"Steve" forKey:@"playerName"];5 [gameScore setObject:[NSNumber numberWithBool:NO] forKey:@"cheatMode"];6 [gameScore save];
1 //保存对象到服务器2 [gameScore save];3 //后台保存4 [gameScore saveInBackground];5 [gameScore saveInBackgroundWithBlock:^(BOOL successed, NSError* error){/*do somthing*/}];e
1 //更新2 [gameScore refresh]
1 //删除对象2 [gameScore deleteInBackground];3 [gameScore deleteInBackgroundWithBlock:...];4 [gameScire delete]//不推荐
1 //如果有objectId的话2 AVQuery *query = [AVQuery queryWithClassName:@"GameScore"];3 AVObject *gameScore = [query getObjectWithId:@"51a90302e4b0d034f61623b5"];4 //访问对象的属性5 int score = [[game Score objectForKey:"score"]intValue];
- 计数器和数组
1 //计数器2 [gameScore incrementKey:@"score"];3 [gameScore saveInBackground];4 //数组 添加元素到数组末尾5 [gameScore addOject:"100" forKey:"some"];6 //添加元素到对应位置,如果字段不存在,就创建7 [gameSocre addUniqueObject:""forKey"ss"];8 //移除9 [gameScore removeObject: forKey:];
- 对象离线存储和离线情况下从缓存加载
1 // Create the object. 2 AVObject *gameScore = [AVObject objectWithClassName:@"GameScore"]; 3 [gameScore setObject:[NSNumber numberWithInt:1337] forKey:@"score"]; 4 [gameScore setObject:@"Sean Plott" forKey:@"playerName"]; 5 [gameScore setObject:[NSNumber numberWithBool:NO] forKey:@"cheatMode"]; 6 [gameScore setObject:[NSArray arrayWithObjects:@"pwnage", @"flying", nil] forKey:@"skills"]; 7 [gameScore saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 8 9 [gameScore setObject:[NSNumber numberWithBool:YES] forKey:@"cheatMode"];10 [gameScore setObject:[NSNumber numberWithInt:1338] forKey:@"score"];11 [gameScore saveEventually];12 }];
AVQuery *query = [AVQuery queryWithClassName:@"GameScore"];query.cachePolicy = kPFCachePolicyNetworkElseCache;/*kPFCachePolicyIgnoreCache 查询行为不从缓存中加载,也不会将结果保存到缓存中。kPFCachePolicyIgnoreCache 是默认的缓存策略。kPFCachePolicyCacheOnly 查询行为会忽略网络状况,只从缓存中加载。如果没有缓存的结果,这个策略会导致一个 AVErrorkPFCachePolicyCacheElseNetwork -- 查询行为首先尝试从缓存中加载,如果加载失败, 它会通过网络加载结果。如果缓存和网络中获取的行为都失败了,会有一个 AVErrorkPFCachePolicyNetworkElseCache -- 查询行为首先尝试从网络中加载,如果加载失败, 它会从缓存中加载结果。如果缓存和网络中获取的行为都失败了,会有一个 AVErrorkPFCachePolicyCacheThenNetwork -- 查询首先从缓存中加载,然后从网络加载。在这种情况下, 回调函数会被调用两次,第一次是缓存中的结果,然后是从网络获取的结果。因为它在不同的时间返回两个结果,这个缓存策略不能和 findObjects 同时使用。*///设置缓存有效期query.maxCacheAge = 24*3600;[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { // Results were successfully found, looking first on the // network and then on disk. } else { // The network was inaccessible and we have no cached data for // this query. }}];// Then, elsewhere in your code...AVQuery *query = [AVQuery queryWithClassName:@"GameScore"];query.cachePolicy = kPFCachePolicyNetworkElseCache;[query findObjectsInBackgroundWithTarget:self selector:@selector(findCallback:error:)];
- 关系型数据
1 //1对1 对象关联 2 // Create the post 3 AVObject *myPost = [AVObject objectWithClassName:@"Post"]; 4 [myPost setObject:@"I‘m Smith" forKey:@"title"]; 5 [myPost setObject:@"Where should we go for lunch?" forKey:@"content"]; 6 7 // Create the comment 8 AVObject *myComment = [AVObject objectWithClassName:@"Comment"]; 9 [myComment setObject:@"Let‘s do Sushirrito." forKey:@"content"];10 11 // Add a one-one relation between the Post and Comment12 [myComment setObject:myPost forKey:@"parent"];13 14 // This will save both myPost and myComment15 [myComment saveInBackground];16 17 //也可以不要对象,用objectId关联18 // Add a relation between the Post with objectId "51a902d3e4b0d034f6162367" and the comment19 [myComment setObject:[AVObject objectWithoutDataWithClassName:@"Post" objectId:@"51a902d3e4b0d034f6162367"]20 forKey:@"parent"];21 22 //n对n23 AVUser *user = [AVUser currentUser];24 AVRelation *relation = [user relationforKey:@"likes"];25 [relation addObject:post];26 [user saveInBackground];27 [relation removeObject:post];
第二章、数据查询
第三章、ACL权限控制
第四章、文件
第五章、用户
第六章、地理位置
第七章、调用云代码
第八章、消息推送
第九章、SNS组件
第十章、事件流
第十一章、反馈
第十二章、应用内搜索
第十三章、应用间数据绑定
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。