1

Good Afternoon,

Today I am having some issues with parse.

I have created a UISearchController, loaded my users from parse so I can search for individual ones and I have added a following and unfollowing feature.

My Problem is when I search for a specific user and try to follow him: So I search for a specific user say "test" it shows up as it should, but when I click follow and then go back to parse to see if "I" have followed test I can a different result.

It says I have followed for example "tester" which was the first user created. Its seeming to follow the Cell and now the userId...

After that I manged to get the users in alphabetical order, but same problem here except it follows the first user in alphabetical order for example if I have a username that begins with an "A"!

I'm not sure how to fix this issue, so I'm hoping someone here does..I accept and appreciate all kind of tips and answers!

1 Answers1

0

You need to check if the UISearchController is active inside of didSelectRowAtIndexPath. Right now you are only selecting from the users array instead of from the searchResults array.

I also highly recommend refactoring your code away from using objectIds and use pointers. It is much easier to query against and work with pointers rather than directly accessing their objectIds.

Here's a link to another question that I've previously answered where I give a complete example of using a UISearchController to search by usernames in Parse.. Hope this helps!

Russell
  • 3,099
  • 2
  • 14
  • 18
  • 1
    Hi thanks for your reply, I have seen your post before and found it very helpful! I have updated the code with another piece of code I wrote which gives me the same error...It does follow the correct user when I select one.. –  Dec 04 '15 at 18:12
  • 1
    If you can go through it quickly and see if you notice anything wrong with this one, Ill try and re-write it following your other post! Thanks again –  Dec 04 '15 at 18:13
  • 2
    My guess is it's probably an issue with the data types. Is the `follower` field of your `followers` class a pointer? If so, you should be assigning an the `User` object to it, not the `objectId` attribute. `following["follower"] = PFUser.currentUser()!` instead of `following["follower"] = PFUser.currentUser()!.objectId`. As mentioned, you shouldn't be referencing the objectIds directly in any of the queries or saves. – Russell Dec 04 '15 at 19:26
  • 1
    the follower and following are strings in parse, not pointers.. Is that what I need? –  Dec 04 '15 at 20:10
  • 2
    That's absolutely what you need. Take a look at the Parse Anypic example project. In particular, you want to look at how the `Activity` class is implemented using pointers such as `fromUser` and `toUser`. Anypic is a fantastic example of a complete app and it's definitely worth taking some time to dig through. https://parse.com/tutorials/anypic – Russell Dec 04 '15 at 20:14
  • 1
    Ok thanks ever so much for the help, I'll let you know. –  Dec 04 '15 at 20:17
  • 1
    I've made them both pointers (follower&following). I'm also looking at you other post, but I cannot really seem to make any sense out of it...When I edit it with what you mention above I get some errors and when I fix them and launch my app I get "Invalid key for following, expected *_User, but got string" when I attempt to follow.. It's sort of making sense to me, but I'm just not sure what parts I need to edit! worst part of being a beginner..Can't wait to learn it..! –  Dec 04 '15 at 21:01
  • 1
    I know its cheeky and probably anoying for yourself, but could you point out a few more things so I can complete this part of my project! you don't know how much I would appreciate the help! –  Dec 04 '15 at 21:02
  • 2
    No worries and you will have to change a fair amount. Instead of having a `String` array for objectIds, you will want to have a `PFUser` array. Likewise, instead of directly adding objectIds to it, you will be adding the `PFUser` objects themselves. When querying, instead of saying `whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)` it will just be `whereKey("follower", equalTo: PFUser.currentUser()!)`. The error just looks like one of the above was neglected since it got a `String` but needed a `PFUser` pointer. It's hard to say what else without seeing the updated code – Russell Dec 04 '15 at 21:28
  • 1
    I have updated my code, I'm doing it step by step, Bascially I have followed your post and made a search for the users. But I'm getting this error at query!.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in it says: "cannot convert value of type '([AnyObject]? , NSError?) -> Void' to expected arguement type 'PFQueryArrayResultBlock?' –  Dec 04 '15 at 22:30
  • 1
    I have just tried it with this instead query!.findObjectsInBackgroundWithBlock { (objects, error) -> Void in It works but when I search I get empty Cells –  Dec 04 '15 at 22:36
  • 1
    I think I'm getting blank cells because theres no data in the cellForRowAtIndexPath, but the data the I would usually insert in there does not work. For example: Cell.textlabel?.text = ........[indexPath.row] –  Dec 04 '15 at 22:53
  • 2
    If you are using Swift2 you should change `(objects: [AnyObject]?, error: NSError?) -> Void` to be `(objects: [PFUser]?, error: NSError?) -> Void`. From there, you are getting empty cells because `cellForRowAtIndexPath` is not fully implemented. You should be able to access the data just fine from the `PFUser` data returned. Something like `let userObject = users[indexPath.row]` `Cell.textlabel?.text = userObject["username"]`. Also don't forget to upvote answers/posts when helpful :) – Russell Dec 04 '15 at 23:34
  • 1
    Shall we open a chat here? might be easier –  Dec 04 '15 at 23:43
  • How do you recommend I do the add the following now? shall I show you the code I used before, I might be able to use that? –  Dec 04 '15 at 23:56
  • 1
    I'm not quite sure what you're asking for but you should be able to reuse much of your code from before. The main difference will be assigning the PFUser instead of the objectId. `following["follower"] = PFUser.currentUser()!` instead of `following["follower"] = PFUser.currentUser()!.objectId` – Russell Dec 05 '15 at 00:08
  • Right I have added the following data I think! I've updated above so you can see everything.. I get no errors but when I launch my app and try to search nothing happens, no users load... so its probably to do with the cellforRow again or searchresultscontroller..I cant see what it is though! –  Dec 05 '15 at 00:34
  • 1
    I'm not sure about the nested function you have inside of `loadUsers` but assuming that's good, try using breakpoints to figure out which piece of the puzzle isn't being populated. It's really tough to debug code without knowing all of the details and the comments section unfortunately isn't too great for that =/ – Russell Dec 05 '15 at 01:09
  • Wait I made a mistake, it might actually be working now! –  Dec 05 '15 at 01:28
  • Wow, it's actually working! I can't thank you enough for the help you've given me!! –  Dec 05 '15 at 01:32
  • 1
    Nicely done! Glad to hear it's working and happy to help! – Russell Dec 05 '15 at 02:10
  • Hi Russel, hope its ok if I ask you a quick question! How would & where would I add a activity view when loading during the search? Is this the correct code: let activityView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge) activityView.center = self.view.center activityView.startAnimating() self.view.addSubview(activityView) ? –  Dec 05 '15 at 20:59
  • It could be along those lines. I personally use JGProgressHUD (https://github.com/JonasGessner/JGProgressHUD) for my activity indicators but there are tons of options available. If you're still having questions/problems go ahead and create another SO post for it so other people may benefit from it as well :) – Russell Dec 06 '15 at 00:06
  • 1
    I'll check that out thank you! I will do, I have actually just made one about UISearchcontroller. As I want to search without seeing the tableview background..So when I click on the searchbar it loads a new view! kind of like Instagrams Discovery page searchbar...Check it out if you like ;) –  Dec 06 '15 at 00:12