This file contains 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
func createPeerConnection() { | |
let config = RTCConfiguration() | |
config.iceServers = [RTCIceServer(urlStrings: iceServers)] | |
// Unified plan is more superior than planB | |
config.sdpSemantics = .unifiedPlan | |
// gatherContinually will let WebRTC to listen to any network changes and send any new candidates to the other client | |
config.continualGatheringPolicy = .gatherContinually | |
This file contains 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
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.prefetchDataSource = self | |
} | |
// MARK: - Table View Data Source Prefetching | |
extension UsersTableViewController: UITableViewDataSourcePrefetching { | |
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { | |
print("prefetch rows at index paths: \(indexPaths)") |
This file contains 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
@IBAction func fetch() { | |
guard !isFetchInProgress else { | |
return | |
} | |
isFetchInProgress = true | |
fetchTotalCountIfNeeded { (totalCount, err) in | |
guard err == nil else { | |
print("Error when get total count: \(err!)") |
This file contains 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
private func fetchUsers(completed: @escaping (QuerySnapshot?, Error?)->Void) { | |
let usersDB = Firestore.firestore().collection("users") | |
var query: Query | |
if self.users.count == 0 { | |
query = usersDB | |
.order(by: "firstName") | |
.limit(to: countPerPage) | |
} | |
else { |
This file contains 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
func fetchTotalCountIfNeeded(completed: @escaping (Int?, Error?)->Void) { | |
guard totalCount == 0 else { | |
completed(totalCount, nil) | |
return | |
} | |
let usersCountRef = Firestore.firestore().collection("counters").document("users") | |
usersCountRef.getDocument { (doc, error) in | |
if let error = error { | |
completed(nil, error) |
This file contains 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
@IBAction func populate() { | |
createUsersCounterIfNeeded { (error) in | |
if let error = error { | |
print("Error when creating users counter: \(error)") | |
} | |
else { | |
self.populateUsers(addedCount: self.addedCount) | |
} | |
} | |
} |
This file contains 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
// Create users and update users counter | |
private func populateUsers(addedCount: Int) { | |
let db = Firestore.firestore() | |
db.runTransaction({ (transaction, errorPointer) -> Any? in | |
// Update user counter to added users amount | |
let usersCounterRef = db.collection("counters").document("users") | |
let usersCounterDoc: DocumentSnapshot | |
do { | |
try usersCounterDoc = transaction.getDocument(usersCounterRef) |
This file contains 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
private func createUsersCounterIfNeeded(completed: @escaping (_ error: Error?) -> Void) { | |
let usersCounter = Firestore.firestore().collection("counters").document("users") | |
usersCounter.getDocument { (doc, error) in | |
guard let doc = doc else { | |
completed(error) | |
return | |
} | |
if doc.exists { | |
completed(nil) |
This file contains 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 ViewController: UIViewController { | |
@IBOutlet weak var imageView: UIImageView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 200, height: 200)) | |
let image = renderer.image { (context) in |