Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Last active November 28, 2017 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/9b28917c8ba8e3745cfa777b35f25b34 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/9b28917c8ba8e3745cfa777b35f25b34 to your computer and use it in GitHub Desktop.
Realm Advent Calendar 2016 25日目 Realm Mobile PlatForm beta Swift3 with 実機 ref: https://qiita.com/daisukenagata/items/f4cf06457b17053dbd7f
static let syncHost = "Macのipアドレスを入力"
func setupUI() {
title = "My Tasks"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
navigationItem.leftBarButtonItem = editButtonItem
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add))
}
override func tableView(_ tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func setupRealm() {
// Log in existing user with username and password
let username = "1111@gmail.com" // <--- Update this
let password = "1111" // <--- Update this
SyncUser.logIn(with: .usernamePassword(username: username, password: password, register: false), server: URL(string: "http://Macのipアドレス:9080")!) { user, error in
guard let user = user else {
fatalError(String(describing: error))
}
DispatchQueue.main.async {
// Open Realm
let configuration = Realm.Configuration(
syncConfiguration: SyncConfiguration(user: user, realmURL: URL(string: "realm://Macのipアドレス:9080/~/realmtasks")!)
)
self.realm = try! Realm(configuration: configuration)
// Show initial tasks
func updateList() {
if self.items.realm == nil, let list = self.realm.objects(TaskList.self).first {
self.items = list.items
}
self.tableView.reloadData()
}
updateList()
// Notify us when Realm changes
self.notificationToken = self.realm.addNotificationBlock { _ in
updateList()
}
}
}
}
deinit {
notificationToken.stop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment