I am building an app using parse where users can click in annotation in a map and see a picture of that place.
My problem is with the block button ... It does not work, when the user click on the block button, he should not see any annotation by the user he is blocking but that unfortunately does not work .
Here is my block button :
#IBAction func blocking(_ sender: Any) {
let block = PFObject(className: "blocking")
block["me"] = PFUser.current()?.username
block["poster"] = post?.name
block["posterID"] = post?.id
block.saveInBackground { (succ, error) in
if error != nil {
print("rttot")
} else {
self.performSegue(withIdentifier: "mo", sender: nil)
}
}
}
(note the poster ID is the ID of person who post the the picture and poster is the name of the person who made the post)
This action will start a relationship between the the user and the user who is blocking . now I will not show any post that hold the person I am blocking ID .
let queryy = PFQuery(className: "blocking")
queryy.whereKey("me", equalTo: PFUser.current()?.username)
queryy.findObjectsInBackground { (objects, error) in
if error != nil {
print("error")
} else {
for object in objects! {
if let neww = object.object(forKey: "posterID") {
var query = PFQuery(className: "posts")
query.whereKey("id", notEqualTo: neww)
query.findObjectsInBackground { (objects, error) in
if error != nil {
print("error")
} else {
for object in objects! {
let post = Posts()
post.chosen = object.object(forKey: "chosen") as! String
post.lati = object.object(forKey: "lati") as! Double
post.longi = object.object(forKey: "longi") as! Double
post.image = object.object(forKey: "image") as? PFFile
post.text = object.object(forKey: "text") as! String
post.name = object.object(forKey: "name") as! String
post.uid = object.object(forKey: "uid") as! String
post.id = object.object(forKey: "id") as! String
print("999")
var cord = CLLocationCoordinate2D(latitude: post.lati, longitude: post.longi)
let anno = annoClass(cord: cord, post: post, text: "Destination", subText: "ClickMe")
self.myMap.addAnnotation(anno)
}
}
}
}
}
}
}
As you can see it will take the ID of people I am blocking and not showing their posts in the map but that does not work . The app does not show any error or bug btw.
Try This
queryy.whereKey("User_has_Blooked", equalTo: false) // if its equal to true, it should not show the users. make this object in the same class when you fetch users in the map!
Related
I'm trying to upload an image to Parse Server usinf PFFile. I'm not having issues uploading it manually from Parse Dashboard and retrieving it from the app. But if I try to upload a new one with this method, the image is not being updated.
The rest of fields are beeing uploaded correctly (name, lastname, username and email)
Save method:
#IBAction func saveBtnPressed(_ sender: Any) {
print("Start saving...")
let imageData = UIImagePNGRepresentation(profilePic.image!)
print ("imageData value:")
print(imageData!)
let imageFile = PFFile(name:"avatar.png", data:imageData!)
print ("imageFile value:")
print(imageFile!)
let query = PFQuery(className: "_User")
query.whereKey("username", equalTo: (PFUser.current()?.username)!)
query.findObjectsInBackground(block: { (objects, error) in
if let user = objects {
for object in user {
object["name"] = self.nameFld.text
object["lastname"] = self.lastnameFld.text
object["username"] = self.emailFld.text
object["email"] = self.emailFld.text
object["avatar"] = imageFile
print(object)
object.saveInBackground()
}
}
})
}
Output:
Start saving...
imageData value:
8358983 bytes
(lldb)
To make this work you will need to upload picture only first and then reference it to the user
let imageData = UIImagePNGRepresentation(profilePic.image!)
print ("imageData value:")
print(imageData!)
let imageFile = PFFile(name:"avatar.png", data:imageData!)
print ("imageFile value:")
print(imageFile!)
imageFile.saveInBackground { (result, error) in
if let error = error{
print(error)
}else{
let query = PFQuery(className: "_User")
query.whereKey("username", equalTo (PFUser.current()?.username)!)
query.findObjectsInBackground(block: { (objects, error) in
if let user = objects {
for object in user {
object["name"] = self.nameFld.text
object["lastname"] = self.lastnameFld.text
object["username"] = self.emailFld.text
object["email"] = self.emailFld.text
object["avatar"] = imageFile
print(object)
object.saveInBackground()
}
}
})
}
}
}
Be aware that in the above example the file is always uploaded to the server even if no users match the query so consider uploading file only after getting query results.
There is no need to query current user. You can do it as follows:
#IBAction func saveBtnPressed(_ sender: AnyObject) {
let userToUpdate = PFUser.current()!
userToUpdate["name"] = self.nameFld.text
userToUpdate["email"] = self.emailFld.text
userToUpdate["username"] = self.emailFld.text
// Save Avatar
if profilePic.image != nil {
let imageData = UIImageJPEGRepresentation(profilePic.image!, 0.5)
let imageFile = PFFile(name:"avatar.jpg", data:imageData!)
userToUpdate["avatar"] = imageFile
}
// Saving block
userToUpdate.saveInBackground(block: { (succ, error) in
if error == nil {
print("Your Profile has been updated!")
} else {
print("Failed")
}})
}
I am trying to run 4 queries from my parse database, each pulling just 1 object. I am trying to add each of the objects to an array that can then be used in a collection view. The queries run successfully but the app crashes because it is pulling null values. Here is my code:
var query1desc = ""
var query2desc = ""
var descs = [String]()
fileprivate func fetchUsers() {
let query1 = PFQuery(className: "Messages")
query1.limit = 1
query1.findObjectsInBackground{
(objects, error) -> Void in
if error == nil {
if objects?.count == 0 {
} else {
let object = objects![0] as! PFObject
self.query1desc = object["message"] as! String
}
}
}
let query2 = PFQuery(className: "Messages")
query2.limit = 1
query2.findObjectsInBackground{
(objects, error) -> Void in
if error == nil {
if objects?.count == 0 {
} else {
let object = objects![0] as! PFObject
self.query2desc = object["message"] as! String
}
}
}
self.descs = [self.query1desc, self.query2desc]
self.collectionView.reloadData()
Does anyone know of a way to fix this so that self.descs does not just provide nil values? When I print within the query itself I know that objects are being pulled. Thanks in advance.
I think desc variable holds nil value because your program is executing an array assignment code part while completion handlers are still processing query requests. You need to wait until all your query requests finish and then can collect all the results.
let workGroup = DispatchGroup()
let queueForQuery1 = DispatchQueue(label: "firstQuery")
let query1 = PFQuery(className: "Messages")
query1.limit = 1
workGroup.enter()
queueForQuery1.async(group:workGroup) {
query1.findObjectsInBackground{ (objects, error) -> Void in
if error == nil {
if objects?.count == 0 {
} else {
let object = objects![0] as! PFObject
self.query1desc = object["message"] as! String
}
}
workGroup.leave()
}
}
let queueForQuery2 = DispatchQueue(label: "secondQuery")
let query2 = PFQuery(className: "Messages")
query2.limit = 1
workGroup.enter()
queueForQuery2.async(group:workGroup) {
query2.findObjectsInBackground{ (objects, error) -> Void in
if error == nil {
if objects?.count == 0 {
} else {
let object = objects![0] as! PFObject
self.query1desc = object["message"] as! String
}
}
workGroup.leave()
}
}
workGroup.notify(queue: DispatchQueue.main){
self.descs = [self.query1desc, self.query2desc]
self.collectionView.reloadData()
}
I got my app onto the App Store. Everything was working fine on my end, and apparently on the reviewers end.
After the app went live, some users reported crashing immediately after they log in with Facebook. Here's the code for where I think it's crashing (run right after users log in with Facebook):
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
protocol getUserDataDelegate {
func gotData()
}
public var userEmailForMixpanel = ""
public var userNameForInvites = ""
class GetUserData: NSObject {
var facebookid:String = ""
var userEmail:String = ""
var userFirstName:String = ""
var userLastName: String = ""
var userGender:String = ""
var userBirthday:String = ""
var delegate = getUserDataDelegate?()
func returnUserData() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, email, first_name, last_name, gender, birthday"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
print("Error: \(error)")
}
else
{
self.facebookid = (result.valueForKey("id") as? String)!
self.userEmail = (result.valueForKey("email") as? String)!
self.userFirstName = (result.valueForKey("first_name") as? String)!
self.userLastName = (result.valueForKey("last_name") as? String)!
self.userGender = (result.valueForKey("gender") as? String)!
//self.userBirthday = (result.valueForKey("birthday") as? String)!
userEmailForMixpanel = self.userEmail
userNameForInvites = self.userFirstName
NSUserDefaults.standardUserDefaults().setValue(userEmailForMixpanel, forKey: "userEmail")
NSUserDefaults.standardUserDefaults().setValue(userNameForInvites, forKey: "userName")
NSUserDefaults.standardUserDefaults().synchronize()
Mixpanel.sharedInstanceWithToken("abdc")
let mixpanel = Mixpanel.sharedInstance()
mixpanel.registerSuperProperties(["Gender":self.userGender])
print(self.facebookid)
print(self.userEmail)
print(self.userFirstName)
print(self.userLastName)
print(self.userGender)
//print(self.userBirthday)
self.checkIfUserExists()
}
})
}
func checkIfUserExists() {
showTutorial = true
let url:NSURL = NSURL(string: "url")!
let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
let userTokenDataDictionary:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
if userTokenDataDictionary ["token"] != nil {
userAccessToken = (userTokenDataDictionary["token"] as? String)!
NSUserDefaults.standardUserDefaults().setValue(userAccessToken, forKey: "userAccessToken")
NSUserDefaults.standardUserDefaults().synchronize()
print("Token for Existing User:\(userAccessToken)")
self.finishedGettingData()
}
if userTokenDataDictionary ["error"] != nil {
userAccessToken = (userTokenDataDictionary["error"] as? String)!
print(userAccessToken)
print("User needs to be created")
self.createNewUserFromFacebook()
}
}
task.resume()
}
func createNewUserFromFacebook() {
let url:NSURL = NSURL(string: "url")!
print(url)
let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
let userTokenDataDictionary:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
if userTokenDataDictionary ["token"] != nil {
userAccessToken = (userTokenDataDictionary["token"] as? String)!
NSUserDefaults.standardUserDefaults().setValue(userAccessToken, forKey: "userAccessToken")
NSUserDefaults.standardUserDefaults().synchronize()
}
if userTokenDataDictionary ["error"] != nil {
userAccessToken = (userTokenDataDictionary["error"] as? String)!
print(userAccessToken)
}
print("Token for New User:\(userAccessToken)")
self.finishedGettingData()
}
task.resume()
}
func checkIfUserHasUsedListenerApp() {
let accessToken = NSUserDefaults.standardUserDefaults().stringForKey("userAccessToken")!
let url:NSURL = NSURL(string: "url")!
print(url)
let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
let adDataDict:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
if adDataDict ["used_ListenerApp"] != nil {
let responseCode = adDataDict.valueForKey("used_ListenerApp") as! Bool
print(responseCode)
if responseCode == false {
Mixpanel.sharedInstanceWithToken("abc")
let mixpanel = Mixpanel.sharedInstance()
mixpanel.track("New User Signed Up", properties: ["distinct_id":userEmailForMixpanel])
}
if responseCode == true {
return
}
}
}
task.resume()
}
func finishedGettingData() {
self.checkIfUserHasUsedListenerApp()
Mixpanel.sharedInstanceWithToken("abc")
let mixpanel = Mixpanel.sharedInstance()
mixpanel.track("User Logged In", properties: ["distinct_id":userEmailForMixpanel])
if let actualdelegate = self.delegate {
actualdelegate.gotData()
}
}
}
It's only crashing for some users, not all. I even tried creating a loop that generates a bunch of user data to run it through this code, and I couldn't replicate the crash.
Any advice would be appreciated a lot.
UPDATE
It looks like it has to do with Facebook not returning an email address. I'll keep looking though.
I figured it out. Part of the problem was that Facebook wasn't returning an email address for some users, so I checked for that and if it didn't return an email, I created one with their Facebook ID to get them through the login process (fbid#facebook.mywebsite.com).
Another part of the problem was that some users had logged into my website using an email address that was also assigned to their Facebook account, and tried logging in with Facebook on the app. I fixed this by having it merge their Facebook info with their existing account during the authorization process if an existing account is found.
Well I'm trying to save the results of a query and when I try to save it in an array it just doesn't do it.
Here's my code:
let query = PFUser.query()
query?.orderByDescending("puntaje")
query?.limit = 50
query?.findObjectsInBackgroundWithBlock({
(users, error) -> Void in
if let objects = users {
for object in objects {
self.usernames.removeAll(keepCapacity: true)
self.scores.removeAll(keepCapacity: true)
if let user = object as? PFUser {
print(user.username)
self.usernames.append(user.username!)
self.scores.append((user["puntaje"] as? Int)!)
}
}
}
print(self.usernames.count)
})
while printing user.username appears all the usernames.
and in the print it shows that I have 0 usernames.
You need to move
self.usernames.removeAll(keepCapacity: true)
self.scores.removeAll(keepCapacity: true)
above the "for" loop. Right under
if let objects = users {
I found another way.
I used:
let query = PFUser.query()
query?.orderByDescending("puntaje")
query?.limit = 50
do {
if let users = try query?.findObjects() {
for user in users as! [PFUser] {
let name = user.username!
self.usernames.append(name)
self.scores.append((user["puntaje"] as? Int)!)
}
}
}catch {
print(error)
}
I have successfully written the code to download the parse objects, fetch the current objects in my database and then compare.
My algorithm:
I iterate through the parse objects and run a fetchrequest and compare their objectID's. If I get nothing, I make a new object for my database. Otherwise I then look at the modifiedDate I have in my database and the updatedAt from parse and compare to see if I need to set new values. This code works great.
The code:
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
let object = object as! PFObject
let name = object["name"] as! String
let email = object["email"] as! String
let subjectsTaught = object["subjectsTaught"] as [String: String]
let category = object["category"] as! String
let uniqueID = object.objectId!
let modifiedDate = object.updatedAt!
let fetchRequest2 = NSFetchRequest(entityName: "Teacher")
fetchRequest2.predicate = NSPredicate(format: "uniqueID == %#", uniqueID)
var error2: NSError?
if let foundTeachers = self.managedObjectContext.executeFetchRequest(fetchRequest2, error: &error2) as? [Teacher] {
if foundTeachers.isEmpty == true {
let teacher = NSEntityDescription.insertNewObjectForEntityForName("Teacher", inManagedObjectContext: self.managedObjectContext) as! Teacher
teacher.name = name
teacher.email = email
teacher.subjectsTaught = subjectsTaught
teacher.category = category
teacher.uniqueID = uniqueID
teacher.modifiedDate = modifiedDate
} else {
if let teacher = foundTeachers.first {
let date1 = teacher.modifiedDate
let date2 = modifiedDate
let compareResult = date1.compare(date2)
if compareResult == NSComparisonResult.OrderedAscending {
teacher.setValue(name, forKey: "name")
teacher.setValue(email, forKey: "email")
teacher.setValue(subjectsTaught, forKey: "subjectsTaught")
teacher.setValue(category, forKey: "category")
teacher.setValue(modifiedDate, forKey: "modifiedDate")
}
}
}
}
var error: NSError?
if !self.managedObjectContext.save(&error) {
println("Error \(error)")
abort()
}
}
}
My question is how should I figure out which objects where not in parse? I don't want to query parse for every object in my database as I assume that would be network intensive.
Should I do a fetchrequest for all Teacher objects in the beginning and as I iterate through the parse objects, delete them as I go? If I have objects left, those should be deleted?
Okay, I figured out what to do. I ended up running a fetchrequest first for all teacher and appending their names to an array. During the parse iteration, I deleted teachers from that list as I went through them and at the end, used that list to delete teachers from the database.
let fetchRequest = NSFetchRequest()
fetchRequest.entity = NSEntityDescription.entityForName("Teacher", inManagedObjectContext: self.managedObjectContext)
var error: NSError?
var foundTeacherNames = [String]()
if let foundTeachers = self.managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as? [Teacher] {
for teacher in foundTeachers {
foundTeacherNames.append(teacher.name)
}
}
//Find teachers in parse database
let query = PFQuery(className: "TeacherList")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
let object = object as! PFObject
let name = object["name"] as! String
let email = object["email"] as! String
let subjectsTaught = object["subjectsTaught"] as! [String: String]
let category = object["category"] as! String
let uniqueID = object.objectId!
let modifiedDate = object.updatedAt!
let fetchRequest2 = NSFetchRequest(entityName: "Teacher")
fetchRequest2.predicate = NSPredicate(format: "uniqueID == %#", uniqueID)
var error2: NSError?
if let foundTeachers = self.managedObjectContext.executeFetchRequest(fetchRequest2, error: &error2) as? [Teacher] {
if foundTeachers.isEmpty == true {
let teacher = NSEntityDescription.insertNewObjectForEntityForName("Teacher", inManagedObjectContext: self.managedObjectContext) as! Teacher
teacher.name = name
teacher.email = email
teacher.subjectsTaught = subjectsTaught
teacher.category = category
teacher.uniqueID = uniqueID
teacher.modifiedDate = modifiedDate
} else {
if let teacher = foundTeachers.first {
let date1 = teacher.modifiedDate
let date2 = modifiedDate
let compareResult = date1.compare(date2)
if compareResult == NSComparisonResult.OrderedAscending {
teacher.setValue(name, forKey: "name")
teacher.setValue(email, forKey: "email")
teacher.setValue(subjectsTaught, forKey: "subjectsTaught")
teacher.setValue(category, forKey: "category")
teacher.setValue(modifiedDate, forKey: "modifiedDate")
}
}
}
if contains(foundTeacherNames, name) {
let i = find(foundTeacherNames, name)!
foundTeacherNames.removeAtIndex(i)
}
}
var error: NSError?
if !self.managedObjectContext.save(&error) {
println("Error \(error)")
abort()
}
if !foundTeacherNames.isEmpty {
for teacher in foundTeacherNames {
let request = NSFetchRequest(entityName: "Teacher")
request.predicate = NSPredicate(format: "name = %#", teacher)
if let fetchResults = self.managedObjectContext.executeFetchRequest(request, error: nil) as? [NSManagedObject] {
if fetchResults.count != 0 {
self.managedObjectContext.deleteObject(fetchResults[0])
}
}
}
}
Yes, the best way is to fetch all entities and then check for the unique ids. You could use key-value-coding (or its Swift equivalents such as map) to just get the ids you are interested in.
let existingIDs = entitiesFromParse.map() { $0.uniqueID as? String }
You can then check if an ID exists with
let idExists = existingIDs.contains(idToCheck)
This is preferable to multiple fetch requests which are expensive.