Created
August 6, 2016 05:32
-
-
Save niwatako/b0446e33e3f98bd93221c6e37e7cac21 to your computer and use it in GitHub Desktop.
TimelineViewControllerに2つメンバ変数を追加、viewDidLoadに処理を追加 #CodePiece #realm_swift #realm_jp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimelineViewController: UITableViewController { | |
var timeline: Results<Tweet>? | |
var notificationToken: NotificationToken? | |
var account: ACAccount? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// アカウントを取得してメンバ変数 var account: ACAccount? に格納 | |
let accountStore = ACAccountStore() | |
let accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter) | |
accountStore.requestAccessToAccountsWithType(accountType, options: nil) { (granted, error) -> Void in | |
if granted { | |
let accounts = accountStore.accountsWithAccountType(accountType) | |
if let account = accounts.first as? ACAccount { | |
self.account = account | |
self.getHomeTimeline() | |
} else { | |
self.showAlert("No Twitter account") | |
} | |
} else { | |
self.showAlert("No account access") | |
} | |
} | |
// TableViewにTimelineCellを表示する準備 | |
tableView.registerNib(UINib(nibName: "TimelineCell", bundle: nil), forCellReuseIdentifier: "timelineCell") | |
tableView.rowHeight = 90 | |
tableView.estimatedRowHeight = 90 | |
// RealmからcreatedAtでソートしたタイムラインを取得するクエリを発行 | |
let realm = try! Realm() | |
timeline = realm.objects(Tweet).sorted("createdAt", ascending: false) | |
// クエリに変更があった場合に通知を受けて実行したい処理を登録 | |
notificationToken = timeline?.addNotificationBlock { [weak self] (change) in | |
switch change { | |
case .Initial(_): | |
self?.tableView.reloadData() | |
case .Update(_, deletions: _, insertions: _, modifications: _): | |
self?.tableView.reloadData() | |
case .Error(_): | |
return | |
} | |
} | |
} | |
// 中略 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment