ParseのiOSのドキュメントを読んでみたメモ (2)#ハツカソンの続き

ParseのiOSのドキュメントを読んでみたメモ (2)#ハツカソンの続きParseのiOSのドキュメントを読んでみたメモ(1) #ハツカソン – Morizotter Blogの続き。公開メモ。公開したほうがモチベーションが上がるので、、、。

Queriesの続き

Relational Queries

これはよく使うことになりそう。

// Assume PFObject *myPost was previously created.
PFQuery *query = [PFQuery queryWithClassName:@"Comment"];
[query whereKey:@"post" equalTo:myPost];
 
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
    // comments now contains the comments for myPost
}];

Caching Queries

query.cachePolicyでキャッシュを使える。キャッシュのポリシーは下記の通り。

Parse provides several different cache policies:

kPFCachePolicyIgnoreCache 
The query does not load from the cache or save results to the cache. kPFCachePolicyIgnoreCache is the default cache policy.
kPFCachePolicyCacheOnly 
The query only loads from the cache, ignoring the network. If there are no cached results, that causes a PFError.
kPFCachePolicyNetworkOnly 
The query does not load from the cache, but it will save results to the cache.
kPFCachePolicyCacheElseNetwork 
The query first tries to load from the cache, but if that fails, it loads results from the network. If neither cache nor network succeed, there is a PFError.
kPFCachePolicyNetworkElseCache 
The query first tries to load from the network, but if that fails, it loads results from the cache. If neither network nor cache succeed, there is a PFError.
kPFCachePolicyCacheThenNetwork 
The query first loads from the cache, then loads from the network. In this case, the callback will actually be called twice - first with the cached results, then with the network results. Since it returns two results at different times, this cache policy cannot be used synchronously with findObjects.

Subclasses

Subclassing PFObject

  • サブクラスにするためには
    1. PFSubclassingプロトコルに準拠する
    2. parseClassNameクラスメソッドを作る
    3. .mファイルにPFObject+Subclassをインポートする
    4. [YourClass registerSubclass]setApplicationId:clientKey:の前にコールする。
  • サブクラスにした時は、プロパティに対応して.mファイルに@dynamicを書いておく。

Files

The PFFile

  • イメージや、ドキュメントやビデオや音楽などを保存するときに使う。
  • up to 10 megabytes

簡単な使い方

let str = "Working at Parse is great!"
let data = str.dataUsingEncoding(NSUTF8StringEncoding)
let file = PFFile(name:"resume.txt", data:data)
file.saveInBackground()

Progress

let str = "Working at Parse is great!"
let data = str.dataUsingEncoding(NSUTF8StringEncoding)
let file = PFFile(name:"resume.txt", data:data)
file.saveInBackgroundWithBlock {
  (succeeded: Bool!, error: NSError!) -> Void in
  // Handle success or failure here ...
}, progressBlock: {
  (percentDone: Int) -> Void in
  // Update your progress spinner here. percentDone will be between 0 and 100.
}

よい。

Config

Parseのコンソール上で設定した値を受け取れる。よい。Fetchに失敗したら、[PFConfig currentConfig]で端末のに保存してある値が利用される。

NSLog("Getting the latest config...");
PFConfig.getConfigInBackgroundWithBlock {
  (var config: PFConfig!, error: error!) -> Void in
  if (!error) {
    NSLog("Yay! Config was fetched from the server.")
  } else {
    NSLog("Failed to fetch. Using Cached Config.")
    config = PFConfig.currentConfig()
  }
 
  var welcomeMessage: NSString? = config["welcomeMessage"] as? NSString
  if welcomeMessage == nil {
    NSLog("Falling back to default message.")
    welcomeMessage = "Welcome!";
  }
 
  NSLog("Welcome Message = %@", welcomeMessage!)
}];

Crash Reporting

ParseCrashReporting.enable();

これをParseのキーをセットする前にかけば良い。

その他メモ

  • Parse CLIってのがあるみたい。いい!

Pocket
LINEで送る

You may also like...