LeanCloud存取数据

1、LeanCloud网址:
https://leancloud.cn/docs/start.html
 https://leancloud.cn/docs/leanstorage_guide-ios.html

2、Pod设置

platform :ios, '7.0'
pod 'AVOSCloud', '~> 3.2.7'
pod 'AVOSCloudIM', '~> 3.2.7'
pod 'AVOSCloudCrashReporting', '~> 3.2.7'

 3、AppDelegate.h

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

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //如果使用美国站点,请加上这行代码 [AVOSCloud useAVCloudUS];
    [AVOSCloud setApplicationId:@"2xSWLp7fz7w1DUyH3m9DqXx8-gzGzoHsz"
                      clientKey:@"aaHsJkan0BxlGY9c9Wdf0HY9"];
    [AVAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
    return YES;
}

 4、AVObject实例化

AVObject *todo = [AVObject objectWithClassName:@"Todo"];
    // 也可以是用下面的方式调用实例方法来创建一个对象
  AVObject *todo = [[AVObject alloc] initWithClassName:@"Todo"];
  // 以上两行代码是完全等价的

 5、存储数据到服务端 并获取到objectId

    /*!
     *  @brief 支持的类型
     */
    NSNumber *boolean = @(YES);
    NSNumber *number = [NSNumber numberWithInt:2014];
    NSString *string = [NSString stringWithFormat:@"famous film name is %@", number];
    NSDate *date = [NSDate date];
    NSData *data = [@"fooBar" dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *array = [NSArray arrayWithObjects:string, number, nil]; // NSArray
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                number,@"number",
                                string, @"string",nil];
    // NSDictionary
    
    AVObject *testObject = [AVObject objectWithClassName:@"DataTypeTest"];
    [testObject setObject:boolean    forKey:@"testBoolean"];
    [testObject setObject:number     forKey:@"testInteger"];
    [testObject setObject:string     forKey:@"testString"];
    [testObject setObject:date       forKey:@"testDate"];
    [testObject setObject:data       forKey:@"testData"];
    [testObject setObject:array      forKey:@"testArray"];
    [testObject setObject:dictionary forKey:@"testDictionary"];
    [testObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            // 存储成功
            NSLog(@"%@",testObject.objectId);// 保存成功之后,objectId 会自动从服务端加载到本地
            self.tv_save.text = testObject.objectId;
        } else {
            // 失败的话,请检查网络环境以及 SDK 配置是否正确
        }
    }];

 6、根据objectId访问对象属性

AVQuery *query = [AVQuery queryWithClassName:@"DataTypeTest"];
    [query getObjectInBackgroundWithId:@"56f8aab879bc44005920635e" block:^(AVObject *object, NSError *error) {
        // object 就是 id 为 56f8aab879bc44005920635e 的 Todo 对象实例
        NSString *location = [object objectForKey:@"localData"];
        NSLog(@"%@\n\n%@",object,location);
        self.tv_query.text = [NSString stringWithFormat:@"%@",location];
        
        //        // 获取三个特殊属性
        //        NSString *objectId = object.objectId;
        //        NSDate *updatedAt = object.updatedAt;
        //        NSDate *createdAt = object.createdAt;
    }];

 

posted @ 2016-03-28 15:00  索马里猫  阅读(1113)  评论(0编辑  收藏  举报