Querying private database in swift for user details - swift

I'm trying to save and store the user's data, then retrieve it and check for a value, pushing the corresponding view controller.
However, despite only having 4 user records in my cloudkit dashboard, i'm getting 33 results, forcing me to change my code and preventing it from working.
This was my original code:
let container = CKContainer.defaultContainer()
let privateDB = container.privateCloudDatabase
let resultPredicate = NSPredicate(format: "TRUEPREDICATE")
let query = CKQuery(recordType: "UserData", predicate: resultPredicate)
query.sortDescriptors = [NSSortDescriptor(key: "MODIFIED", ascending: false)]
privateDB.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
if error != nil {
print("\(error)")
}
else{
for record in results! {
self.weight = record["weight"] as? Int
self.height = record["height"] as? Int
self.age = record["age"] as? Int
self.gender = record["genderFemale"] as? Int
if self.weight == nil {
print("push weightVC")
let weightVC = WeightViewController()
self.navigationController?.pushViewController(weightVC, animated: false)
}
else if self.height == nil {
print("push heightVC")
let heightVC = HeightViewController()
self.navigationController?.pushViewController(heightVC, animated: false)
}
else if self.age == nil {
print("push ageVC")
let ageVC = DOBViewController()
self.navigationController?.pushViewController(ageVC, animated: false)
}
else if self.gender == nil{
print("push genderVC")
let genderVC = GenderViewController()
self.navigationController?.pushViewController(genderVC, animated: false)
}
else{
let planVC = PlanOriginViewController()
self.navigationController?.pushViewController(planVC, animated: false)
}
I was forced to change it to this:
privateDB.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
if error != nil {
print("\(error)")
}
else{
print("\(results?.count)")
for record in results! {
self.weight = record["weight"] as? Int
self.height = record["height"] as? Int
self.age = record["age"] as? Int
self.gender = record["genderFemale"] as? Int
self.arrayOfUserData?.addObject(record)
print("record added")
}
}
}
print(arrayOfUserData)
if arrayOfUserData != nil{
let ckRecord = arrayOfUserData![0]
self.weight = ckRecord["weight"] as? Int
self.height = ckRecord["height"] as? Int
self.age = ckRecord["age"] as? Int
self.gender = ckRecord["genderFemale"] as? Int
if self.weight == nil {
print("push weightVC")
let weightVC = WeightViewController()
self.navigationController?.pushViewController(weightVC, animated: false)
}
else if self.height == nil {
print("push heightVC")
let heightVC = HeightViewController()
self.navigationController?.pushViewController(heightVC, animated: false)
}
else if self.age == nil {
print("push ageVC")
let ageVC = DOBViewController()
self.navigationController?.pushViewController(ageVC, animated: false)
}
else if self.gender == nil{
print("push genderVC")
let genderVC = GenderViewController()
self.navigationController?.pushViewController(genderVC, animated: false)
}
else{
let planVC = PlanOriginViewController()
self.navigationController?.pushViewController(planVC, animated: false)
}
} else {
}
However, this doesn't work as well. XCode is skipping over the privateDB query block and going straight to the line print(arrayOfUserData), before returning to the privateDB query.
Thanks!

You issue is that you are assuming that your file is single threaded. What you want it to evaluate What fields are not filled in once you have received a results from the database query. What you need is a completion block. This lets you query the data base and then evaluate the results after. I would do something around these lines.
first add a completion handler that gives you a user object back. I assume you have a user defined should look something like this
struct User{
weight:Int
height:Int
age:Int
gender:Sting
}
add your completion handler in your Query Method.
privateDB.performQuery(query, inZoneWithID: nil, completion:([User])) { (results, error) -> Void in
let resutltData = results.value as [String:AnyObject] ?? [:]
let users : [User] = resutltData.flatMap { record in
let weight = record["weight"] as? Int ?? ""
let height = record["height"] as? Int ?? ""
let age = record["age"] as? Int ?? ""
let gender = record["gender"] as? String ?? ""
}
completion(user)
}
now when you call privateDB.performQuery you should save it in a variable and preform your checks there. Something like this:
func ProcessUsers(){
let users = privateDB.performQuery(query, inZoneWithID: nil, completion:{ (User) in
self.user = User
switch user{
case .weight:
if .weight == "" {
print("push weightVC")
let weightVC = WeightViewController()
self.navigationController?.pushViewController(weightVC, animated: false) } else {
fallthrough
}
case .height:
if .height == "" {
print("push heightVC")
let heightVC = heightViewController()
self.navigationController?.pushViewController(heightVC, animated: false) } else {
fallthrough
}
case .age:
if .age == "" {
print("push ageVC")
let ageVC = ageViewController()
self.navigationController?.pushViewController(ageVC, animated: false) } else {
fallthrough
}
case .gender:
if .gender == "" {
print("push genderVC")
let genderVC = genderViewController()
self.navigationController?.pushViewController(genderVC, animated: false) } else {
fallthrough
}
Let me know if you have any more questions about this. I hope it helps.

Related

AdMob Native ad loading but not showing

I'm using Google AdMob to display native ads on my app. I made everything as in the Google Documentation. After testing, I figured out that test ad values are loading successfully since they aren't nil and when I print them, I can see them as they should display. However data is not displaying on my GADNativeAdView. Here is some of my code:
var nativeAdView: GADNativeAdView! {
didSet {
nativeAdView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(nativeAdView)
nativeAdView.leftAnchor.constraint(equalTo: collectionView4.leftAnchor).isActive = true
nativeAdView.rightAnchor.constraint(equalTo: collectionView4.rightAnchor).isActive = true
nativeAdView.topAnchor.constraint(equalTo: collectionView4.bottomAnchor, constant: 10).isActive = true
nativeAdView.heightAnchor.constraint(equalToConstant: 310).isActive = true
}
}
var adLoader: GADAdLoader!
// MARK: - Google Mobile Ads
extension HomeController: GADNativeAdLoaderDelegate, GADNativeAdDelegate {
private func setupAd() {
let multipleAdsOptions = GADMultipleAdsAdLoaderOptions()
multipleAdsOptions.numberOfAds = 1
adLoader = GADAdLoader(adUnitID: testNativeId, rootViewController: self,
adTypes: [.native],
options: [multipleAdsOptions])
adLoader.delegate = self
adLoader.load(GADRequest())
}
func imageOfStars(from starRating: NSDecimalNumber?) -> UIImage? {
guard let rating = starRating?.doubleValue else {
return nil
}
if rating >= 5 {
return UIImage(named: "stars_5")
} else if rating >= 4.5 {
return UIImage(named: "stars_4_5")
} else if rating >= 4 {
return UIImage(named: "stars_4")
} else if rating >= 3.5 {
return UIImage(named: "stars_3_5")
} else {
return nil
}
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
// Create and place ad in view hierarchy.
let nibView = Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil)?.first
guard let nativeAdView = nibView as? GADNativeAdView else {
return
}
self.nativeAdView = nativeAdView
nativeAd.delegate = self
(nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
(nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
nativeAdView.bodyView?.isHidden = nativeAd.body == nil
(nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
nativeAdView.callToActionView?.isHidden = nativeAd.callToAction == nil
(nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
nativeAdView.iconView?.isHidden = nativeAd.icon == nil
(nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(from: nativeAd.starRating)
nativeAdView.starRatingView?.isHidden = nativeAd.starRating == nil
(nativeAdView.storeView as? UILabel)?.text = nativeAd.store
nativeAdView.storeView?.isHidden = nativeAd.store == nil
(nativeAdView.priceView as? UILabel)?.text = nativeAd.price
nativeAdView.priceView?.isHidden = nativeAd.price == nil
(nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil
nativeAdView.callToActionView?.isUserInteractionEnabled = false
nativeAdView.nativeAd = nativeAd
}
func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
print("ERROR: \(error.localizedDescription)")
}
}
Here is my .xib file:
The views custom class on the .xib file are set.
After trial and error I figured out that using #IBOutlet for views in the .xib file and calling them on the adLoader(_:) worked.

shared database zones cannot be found

I have sucessfully shared a record from private database customzone.
share url Optional(https://www.icloud.com/share/0N9smwzXZ0gfumZv8jyVo7uag)
When I print shared record I get this:
record completion <CKShare: 0x106702100; participants=(
"<CKShareParticipant: 0x2817115e0; hasProtectionInfo=1, isCurrentUser=YES, participantID=E742CBD3-41C9-4A9E-A392-08914E8C1D37, permission=readWrite, role=owner, acceptanceStatus=Accepted, identity=<CKUserIdentity: 0x281815ce0; lookupInfo=<CKUserIdentityLookupInfo: 0x2832ba910; email=vhrao#icloud.com>, cached=0, nameComponents=viswanatha, userID=<CKRecordID: 0x283c6f940; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>, contactIdentifiers={\n items = (\n );\n}>>"
), allowsReadOnlyParticipantsToSeeEachOther=YES, publicPermission=none, recordChangeTag=2z, rootRecordID=<CKRecordID: 0x283c39360; recordName=645B675E-121F-4DC8-B86D-8968C8DE7B1C-9651-00000ED7A0D0E95D, zoneID=userProfileZone:__defaultOwner__>, routingKey=0N9, recordID=Share-967C18F4-43C4-4958-9D40-6D890DD205AD:(userProfileZone:__defaultOwner__), recordType=cloudkit.share, etag=2z> and nil
record completion <CKRecord: 0x10bc05ac0; recordID=645B675E-121F-4DC8-B86D-8968C8DE7B1C-9651-00000ED7A0D0E95D:(userProfileZone:__defaultOwner__), recordChangeTag=30, values={
asOfDate = "2020-08-01 20:26:44 +0000";
latitude = "29.9288104125185";
locationId = "1D2A93A8-A22C-4B2A-9EC6-86F646667F3E-8282-00000CF78F38B1DB";
locationType = "Household User";
longitude = "-95.6080147578347";
profileImage = "<CKAsset: 0x10bc05f60; referenceSignature=<01ebb50c 4bd0e618 bb0f4486 b486145b 30f22689 83>, uploadRank=0, path=/private~/Library/Caches/CloudKit/0fce42f4be876f65546aa1b4549b3658e9291963/Assets/B21AB25E-C07A-4D65-9238-61A17B3E6372.01b1f7f738f239ee006815df2bb2896fade0f08229, size=34656, UUID=B21AB25E-C07A-4D65-9238-61A17B3E6372, signature=<01b1f7f7 38f239ee 006815df 2bb2896f ade0f082 29>, wrappedAssetKey=<24 bytes>>";
userType = "Consumer";
}, share=<CKReference: 0x283c38ae0; recordID=<CKRecordID: 0x283c390c0; recordName=Share-967C18F4-43C4-4958-9D40-6D890DD205AD, zoneID=userProfileZone:__defaultOwner__>>, recordType=infrastructure> and nil
Then I fetch RecordZones
func fetchRecordZones(completion: #escaping (CKRecordZoneID?, Error?) -> Void) {
var fetchedRecordZones: [CKRecordZoneID : CKRecordZone]? = nil
let fetchZonesOperation = CKFetchRecordZonesOperation.fetchAllRecordZonesOperation()
fetchZonesOperation.fetchRecordZonesCompletionBlock = { (recordZones: [CKRecordZoneID : CKRecordZone]?, error: Error?) -> Void in
guard error == nil else {
completion(nil, error)
return
}
if let recordZones = recordZones {
fetchedRecordZones = recordZones
for recordID in recordZones.keys {
print(recordID.zoneName)
if (recordID.zoneName == Cloud.SharedZone.UserProfile.ZoneName) {
completion(recordID, nil)
}
}
}
completion(nil, nil)
}
fetchZonesOperation.qualityOfService = .utility
self.sharedDB?.add(fetchZonesOperation)
}
I get no recordZones
I go to coud Kit and look at the shared database and fetch zones with fetch with change Token checkbox enabled I get name: userProfileZone owner record name:_9d4825db9b0aa911655e420ec0016129
Looks like it has a zone in shared database. At least seems like, so why am I not getting zones from shared database?
//Sharing code
var lookupInfos = [CKUserIdentity.LookupInfo]()
let creatorID = rootRecord.creatorUserRecordID
self.defaultContainer?.discoverUserIdentity(withUserRecordID: creatorID!) { [weak self] (userIdentity, error) in
guard error == nil else {
if let ckerror = error as? CKError {
self!.aErrorHandler.handleCkError(ckerror: ckerror)
}
completion(false, error)
return
}
if let userIdentity = userIdentity {
lookupInfos.append(userIdentity.lookupInfo!)
let share = CKShare(rootRecord: rootRecord)
share[CKShareTitleKey] = "Infrastructure" as CKRecordValue
//share[CKShareThumbnailImageDataKey] = shoppingListThumbnail as CKRecordValue
share[CKShareTypeKey] = "com.nr2r.infrastructure" as CKRecordValue
if let lookupInfo = userIdentity.lookupInfo {
let op: CKFetchShareParticipantsOperation = CKFetchShareParticipantsOperation(userIdentityLookupInfos: [lookupInfo])
op.fetchShareParticipantsCompletionBlock = { error in
if let error = error {
print("error: ", error)
}
}
op.shareParticipantFetchedBlock = { participant in
participant.permission = .readWrite
share.addParticipant(participant)
let modOp: CKModifyRecordsOperation = CKModifyRecordsOperation(recordsToSave: [rootRecord, share], recordIDsToDelete: nil)
modOp.savePolicy = .ifServerRecordUnchanged
modOp.perRecordCompletionBlock = {record, error in
print("record completion \(record) and \(String(describing: error))")
}
modOp.modifyRecordsCompletionBlock = {records, recordIDs, error in
guard let ckrecords: [CKRecord] = records, let record: CKRecord = ckrecords.first, error == nil else {
print("error in modifying the records " + error!.localizedDescription)
completion(false, error)
return
}
if let records = records {
for record in records {
print ("recordType: \(record.recordType)")
if (record.recordType == "cloudkit.share") {
self!.sharedCKRecordZoneID = record.recordID.zoneID
}
print ("recordName: \(record.recordID.recordName)")
print ("zoneID: \(record.recordID.zoneID.zoneName)")
print ("zoneID: \(record.recordID.zoneID.ownerName)")
}
}
if let anURL = share.url {
print("share url \(String(describing: share.url))")
completion(true, nil)
}
}
self?.privateDB?.add(modOp)
}
self?.defaultContainer?.add(op)
}
} else {
completion(false, nil)
}
}
More edits to show how to retrieve shared record:
let op = CKFetchShareMetadataOperation(shareURLs: [aURL])
op.perShareMetadataBlock = { shareURL, shareMetadata, error in
if let shareMetadata = shareMetadata {
if shareMetadata.participantStatus == .accepted {
let query = CKQuery(recordType: Cloud.Entity.Infrastructure, predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
let zone = CKRecordZoneID(zoneName: Cloud.PrivateZone.UserProfile.ZoneName, ownerName: (shareMetadata.ownerIdentity.userRecordID?.recordName)!)
self.sharedDB?.perform(query, inZoneWith: zone, completionHandler: { (records, error) in
if let ckerror = error as? CKError {
self.aErrorHandler.handleCkError(ckerror: ckerror)
completion(nil, error)
} else if let records = records, let firstRecord = records.first {
completion(firstRecord, nil)
}
})
}
}
Fetching shared database zones return zero
self.sharedDB?.fetchAllRecordZones(completionHandler: { (ckrecordZones, error) in
guard error == nil else {
if let ckError = error as? CKError {
self.aErrorHandler.handleCkError(ckerror: ckError)
}
completion (nil, error)
return
}
if let recordZones = ckrecordZones {
for i in 0 ..< recordZones.count{
// find the zone you want to query
if recordZones[i].zoneID.zoneName == Cloud.SharedZone.UserProfile.ZoneName {
completion (recordZones[i].zoneID, nil)
}
}
}
completion (nil, nil)
})

Swift CloudKit not working with Cellular (Time exceeded)

I'm using following code to fetch Data from iCloud:
func fetchShoppingList() {
let container = CKContainer.default()
let publicDB = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "ShoppingList", predicate: predicate)
let operation = CKQueryOperation(query: query)
operation.allowsCellularAccess = true
operation.qualityOfService = .userInitiated
publicDB.add(operation)
publicDB.perform(query, inZoneWith: nil) { [unowned self] results, error in
if error != nil {
print(error)
}
else {
for var value in results! {
let shoppingListEntry = ShoppingListEntry()
shoppingListEntry.index = value.value(forKey: "index") as! Int
shoppingListEntry.product = value.value(forKey: "product") as! String
shoppingListEntry.amount = value.value(forKey: "amount") as! Int
shoppingListEntry.priority = value.value(forKey: "priority") as! Int
if value.value(forKey: "isSelected") as! String == "true" {
shoppingListEntry.isSelected = true
}
else {
shoppingListEntry.isSelected = false
}
self.shoppingListEntrys.append(shoppingListEntry)
}
OperationQueue.main.addOperation({ () -> Void in
self.tableViewShoppingList.reloadData()
})
}
}
}
Everything works fine if my phone is connected via Wifi, but if I'm using Cellular I get following error: CKError 0x170244e30: "Network Failure" (4/-1001); "Zeitüberschreitung bei der Anforderung." So there seems to be a problem with time exceeding. I looked for a solution and found a post, saying I have to add the operation lines but nothing changed.
Can anybody help me please?

Remove Values Which Are Nil From NSManagedObject

I had an issue where my core data NSManagedObject always had nil values inside of it on the first run of the application. Do overcome this I thought I would attempt to check for nil values and remove them in case they are found. My attempted code is as follow:
do {
let results = try managedContext.fetch(fetchRequest)
scoreData = results as! [NSManagedObject]
print(scoreData)
for result in results as! [NSManagedObject] {
if result.value(forKey: "name") as? String == nil || result.value(forKey: "date") as? String == nil || result.value(forKey: "score") as? String == nil {
managedContext.delete(result.value(forKey: "name") as! NSManagedObject)
managedContext.delete(result.value(forKey: "date") as! NSManagedObject)
managedContext.delete(result.value(forKey: "score") as! NSManagedObject)
} else {
name = result.value(forKey: "name") as! String
date = result.value(forKey: "date") as! String
score = result.value(forKey: "score") as! Int
}
The line of code 'managedContext.delete()' is giving me a hard time as it isnt properly working. Am I using a wrong method or is my code just incorrect. Any help welcome.
class LeaderboardTableVC: UITableViewController {
var finishedGame = 0
var gameScore:Int! = 0
var name:String!
var date:String!
var score:Int!
var scoreData = [NSManagedObject]()
override func viewDidLoad() {
super.viewDidLoad()
if finishedGame == 1{
saveNew()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func saveNew(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Scores", in: managedContext)
let object = NSManagedObject(entity: entity!, insertInto: managedContext)
let enterNameAlert = UIAlertController(title: "Please Enter Your Name", message: "This will be used to place you in the leaderboards", preferredStyle: .alert)
enterNameAlert.addTextField { (textField:UITextField) -> Void in
textField.placeholder = "Name"
textField.autocapitalizationType = UITextAutocapitalizationType.words
textField.autocorrectionType = UITextAutocorrectionType.no
textField.clearsOnBeginEditing = true
textField.clearsOnInsertion = true
textField.clearButtonMode = UITextFieldViewMode.always
textField.keyboardType = UIKeyboardType.default
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (action:UIAlertAction) in
let currentTime = Date()
let timeFormatter = DateFormatter()
timeFormatter.locale = Locale.current
timeFormatter.dateFormat = "HH:mm dd/MM/yy"
let convertedTime = timeFormatter.string(from: currentTime)
let enteredName = enterNameAlert.textFields?.first?.text
object.setValue(convertedTime, forKey: "date")
object.setValue(self.gameScore, forKey: "score")
object.setValue(enteredName, forKey: "name")
do {
try managedContext.save()
self.scoreData.append(object)
self.tableView.reloadData()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
enterNameAlert.addAction(cancelAction)
enterNameAlert.addAction(confirmAction)
self.present(enterNameAlert, animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! LeaderBoardCell
let data = scoreData[(indexPath as NSIndexPath).row]
if data.value(forKey: "date") as? String == nil || data.value(forKey: "score") as? Int == nil || data.value(forKey: "name") as? String == nil {
print("Nil Value")
} else {
cell.dateLabel?.text = "Date: \((data.value(forKey: "date")) as! String)"
cell.dateLabel.adjustsFontSizeToFitWidth = true
cell.scoreLabel?.text = "Score: \((data.value(forKey: "score")) as! Int)"
cell.scoreLabel.adjustsFontSizeToFitWidth = true
cell.nameLabel?.text = "Name: \((data.value(forKey: "name")) as! String)"
cell.nameLabel.adjustsFontSizeToFitWidth = true
}
return cell
}
Thanks
you can simply check for value if there is a value then store it to the variable as below. try the below code.
for result in results as! [NSManagedObject] {
if let name = result.value(forKey: "name") {
self.name = name as! String // if there will be a value in result for key 'name' then it will sore it. otherwise it won't let it be inside the if condition.
}
if let date = result.value(forKey: "date") {
self.date = date as! String
}
if let score = result.value(forKey: "score") {
self.score = score as! Int
}
}
So your problem is that the properties of the managed object are nil, eg they don't exist. Trying to delete the values won't do anything as they already don't exist. Instead you can delete the managed object, like this:
do {
let results = try managedContext.fetch(fetchRequest)
scoreData = results as! [NSManagedObject]
print(scoreData)
for result in results as! [NSManagedObject] {
if result.value(forKey: "name") as? String == nil || result.value(forKey: "date") as? String == nil || result.value(forKey: "score") as? String == nil {
managedContext.delete(result)
} else {
name = result.value(forKey: "name") as! String
date = result.value(forKey: "date") as! String
score = result.value(forKey: "score") as! Int
}
(Don't forget to save the managed context after)
Be careful though! If you have relationships in your model and you delete a managed object the relationship will still exist, meaning when you access the relationship objects there will be nil objects (the ones you delete here)
Edit: to ensure that your tableView is in sync with your dataSource (scoreData) add a didSet function to your variable declaration like so:
var scoreData = [NSManagedObject]() {
didSet {
tableView.reloadData()
}
}
This means that every time you change scoreData the tableView is reloaded (you don't need to reload data in your saveNew function anymore with this)

Array out of index when getting PFFile from Parse

My app gets the images just fine but if I scroll up and down for a while and then pull to refresh, it crashes and says array out of index. This does not happen every time. I believe it has something to do with the fact that I am not using getdatainbackground but when I go to use that instead of getData(), it loops faster than it can actually retrieve the files and they are out of order.
My current code also gives me this error: Break on warnBlockingOperationOnMainThread() to debug.
2016-03-02 00:35:48.630 App Name[1137:96655] Warning: A long-running operation is being executed on the main thread.
if Reachability.isConnectedToNetwork() {
self.skip = 0
//--RESETTING THE ARRAYS FOR DATA--\\
self.contactText.removeAll(keepCapacity: true)
self.names.removeAll(keepCapacity: true)
self.images.removeAll(keepCapacity: true)
self.prices.removeAll(keepCapacity: true)
self.sizes.removeAll(keepCapacity: true)
self.conditions.removeAll(keepCapacity: true)
self.dates.removeAll(keepCapacity: true)
self.ids.removeAll(keepCapacity: true)
self.createdBy.removeAll(keepCapacity: true)
//--RESET THE USERS LOCATION WHEN HE REFRESHES IN CASE OF A DARASTIC MOVE IN LOCATION--\\
let userGeoPoint = PFUser.currentUser()!["location"] as! PFGeoPoint
//--GETTING ALL OF THE OBJECTS WITHIN 60 MILES OF USERS CURRENT LOCATION--\\
let query = PFQuery(className:"Shoes")
query.whereKey("Location", nearGeoPoint: userGeoPoint, withinMiles: 60)
let user = PFUser.currentUser() as PFUser!
let array: AnyObject? = user["blockedUsers"]
if(array != nil){
query.whereKey("createdBy", notContainedIn: array! as! [AnyObject])
}
query.limit = 50
query.orderByDescending("createdAt")
query.skip = self.skip
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
print("Successfully retrieved \(objects!.count) scores.")
for object in objects! {
if let dateCreated = object.createdAt as NSDate? {
self.dates.append(dateCreated)
}
self.contactText.append(object["Contact"] as! String)
self.descriptions.append(object["Description"] as! String)
self.names.append(object["Name"] as! String)
if object["price"] as! String == "" || object["price"] == nil{
self.prices.append("Negotiable")
}else{
self.prices.append(object["price"] as! String)
}
if object["size"] as! String == "" || object["size"] == nil{
self.sizes.append("N/A")
}else{
self.sizes.append(object["size"] as! String)
}
if object["conditionType"] as! String == "" || object["conditionType"] == nil{
self.conditions.append("N/A")
}else{
self.conditions.append(object["conditionType"] as! String)
}
self.ids.append(object.valueForKey("objectId") as! String)
self.createdBy.append(object["createdBy"] as! String)
let imageFile = object["imageFile"] as! PFFile
let imageData = imageFile.getData()
if (imageData != nil) {
let image = UIImage(data:imageData!)
self.images.append(image!)
}
}
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
else {
print(error)
}
self.refresher.endRefreshing()
}
self.skip+=50
} else {
print("Internet connection not available")
self.refresher.endRefreshing()
let alert = UIAlertView(title: "No Internet connection", message: "Please ensure you are connected to the Internet", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
Try adding a check to make sure your arrays aren't empty when assigning data to cells.