Skip to content

Instantly share code, notes, and snippets.

@Sunilkumarr
Last active August 13, 2020 13:37
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 Sunilkumarr/8064a64125b3770e0260ff4ca0f8c742 to your computer and use it in GitHub Desktop.
Save Sunilkumarr/8064a64125b3770e0260ff4ca0f8c742 to your computer and use it in GitHub Desktop.
Get registered users details
1.Add this code in your class
import Applozic
/// Fetchs the contact from local db
func fetchContactsFromDB() -> [ALContact] {
let dbHandler = ALDBHandler.sharedInstance()
let fetchReq = NSFetchRequest<DB_CONTACT>(entityName: "DB_CONTACT")
var predicate = NSPredicate()
fetchReq.sortDescriptors = [NSSortDescriptor(key: "displayName", ascending: true),NSSortDescriptor(key: "userId", ascending: true)]
var contactList = [ALContact]()
fetchReq.returnsDistinctResults = true
if !ALUserDefaultsHandler.getLoginUserConatactVisibility() {
predicate = NSPredicate(format: "userId!=%@ AND deletedAtTime == nil", ALUserDefaultsHandler.getUserId() ?? "")
}
fetchReq.predicate = predicate
do {
let list = try dbHandler?.managedObjectContext.fetch(fetchReq)
if let db = list {
for dbContact in db {
let contact = ALContact()
contact.userId = dbContact.userId
contact.fullName = dbContact.fullName
contact.contactNumber = dbContact.contactNumber
contact.displayName = dbContact.displayName
contact.contactImageUrl = dbContact.contactImageUrl
contact.email = dbContact.email
contact.localImageResourceName = dbContact.localImageResourceName
contact.contactType = dbContact.contactType
contactList.append(contact)
}
}
} catch {
}
return contactList
}
2. For starting loading the users contacts in viewdidload
ALUserDefaultsHandler.setContactServerCallIsDone(false)
/// fetchs the users contact from server
let userService = ALUserService()
userService.getListOfRegisteredUsers(completion: { _ in
/// gets the contact from local db
let contactArray = self.fetchContactsFromDB()
ALUserDefaultsHandler.setContactServerCallIsDone(true)
/// contact deatils
for contact in contactArray as [ALContact] {
print("@@ Contact userId ",contact.userId)
print("@@ display name : ",contact.displayName)
}
})
3. On scrolling load the old next contacts
let userService = ALUserService()
userService.getListOfRegisteredUsers(completion: { _ in
let contactArray = self.fetchContactsFromDB()
ALUserDefaultsHandler.setContactServerCallIsDone(true)
for contact in contactArray as [ALContact] {
print("@@ Contact userId ",contact.userId)
print("@@ display name : ",contact.displayName)
}
})
Note : once the scroll is completed you will have remove all contacts and add the contactArray again in your list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment