PFObject not being updated on IOS device - ios

I have a project that should update a PFObject on Parse.com. It works fine on an IOS simulator, but when I downloaded it onto a phone, the inputs aren't updating in Parse. I've checked that the ID and ClientKey from Parse are still correct, and there are no errors in my code.
Does anyone know what is going on?

Are you making sure to use the .saveInBackgroundWithBlock { (success, error) ... block to save the values in the background when you have processed the information in your app that you want to be saved into your Parse database?
For example,
A following function for a standard social media app would have the following.
func follow(user: PFUser!, completionHandler:(error: NSError?) -> ())
{
// Storing the relation between current user and another user and then saving it in the database under the relationKey "following"
var relation = PFUser.currentUser().relationForKey("following")
relation.addObject(user)
PFUser.currentUser().saveInBackgroundWithBlock { (success, error) -> Void in
// we now have an error option for the signUp function second loop for the user following themself
completionHandler(error: error)
}
}

Related

"THREAD EXEC BREAKPOINT" issue in my code trying to add a document into firestore subcollection

I am trying to add a document into a subcollection in a document found in my firestore database via a function. But unfortunately it causes a thread exec error which I clearly can't tell what the issue is.
I have used this same method of proceeding in other functions of this type using the documents unique id as point of reference to a particular document. But I can't tell why this particular case bugs
Code bug :
func addComment(from data:[String:Any], completion: #escaping (Bool) -> ()){
db.collection("Events").document((event?.uid)!).collection("Comments").addDocument(data: data) { (error) in // This line causes Thread Breakpoint
if error != nil {
print(error?.localizedDescription)
return
}
}
completion(true)
}
It should add a document with an auto-generated id set with the data parsed in

How to synchronously retrieve PFUser objectForKey in Swift

I am trying to synchronously retrieve a PFUser object information. However, I've tried using fetch, but I keep on getting implementation errors. I would like to find for example: PFUser.currentUser()?.objectForKey("scoreNumber"), but I am not getting the updated value. I was told I would find it using fetch, but I am unable to implement objectForKey in my fetch function.
Some of the attempts
var score: String!
func retrieveScore(){
guard let myUser = PFUser.currentUser() else { return }
do{ let data = try myUser.objectForKey("scoreNumber")?.fetch()
try scoreNumber = data! as! String
}catch{}
}
Another one
let userScore = PFUser.currentUser()?.objectForKey("scoreNumber")
let userScore2 = userScore.fetch()
You shouldn't save the user for NSUserDefaults. Parse SDK does a good job of managing your sessions for you. Just have a specific log out button that calls the [PFUser logout]` function.
So, fetch is an asynchronous operation. This means that it runs on a separate thread, and then the main thread will continue executing the next commands. So, you're calling fetch, which fetches an object in the background, but then you are trying to access a value from the object that hasn't been fetched yet. You need to access the object from within the block handling the results of the fetch call.
When you call fetch the way you did, just with .fetch(), it runs in the background but doesn't alert you when you have the data, nor if the fetch failed. This is not a good way to get data you're going to need immediately.
You need to use fetchInBackgroundWithBlock()
scoreNumber.fetchInBackgroundWithBlock() {
(scoreNumber: PFObject?, error: NSError?) -> Void in
if error == nil && scoreNumber != nil {
print(scoreNumber)
} else {
print(error)
}
}

App Crashes when Motion and Fitness permissions are provided after initial denial

I in my app is requesting user to provide motion and fitness application access to read data such as steps and others. I am currently facing an issue where if the permissions are initially denied by the user and later provided when alerted on loading of the screen.
I have added the error as an image and provided code which I am using to access pedometer class. If someone can provide me an overview about the error it will be really helpful.
if(CMPedometer.isStepCountingAvailable()){
let pedoMeter = CMPedometer()
pedoMeter.startPedometerUpdatesFromDate(timeM) { (data, error) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if(error == nil){
self.stepsTaken.text = String((data!.numberOfSteps))
}
else{
self.alertView()
}
})
}
}

PFUser currentUser not saving in iOS7

I am using the below code to save the currently logged in user with custom field. I allow the user to fill in information and then save. I used both the save methods on my own threading using GCM and used the saveInBackgrounWithBlock. On iOS8, this works ok but on iOS7 saving never happens and the completion block is never called. Any ideas? Thanks
if PFUser.currentUser() != nil {
PFUser.currentUser().setObject(installation, forKey: "installation")
PFUser.currentUser().saveInBackgroundWithBlock({ (bool: Bool, error: NSError?) -> Void in
if(error != nil) {
let alert = UIAlertView(title: "Problem Saving", message: "Make sure you are connecte to the internet and try again", delegate: nil, cancelButtonTitle: "OK")
alert.show();
}
})
}
Update 1: I noticed that deleting the app resolves the issue temporarily. However, after signing out and signing in with other user (i.e. changing the current user), the issue will pop up again.
Update 2: The issue seems to be coming from PFInstallation somehow. Using addUniqueObject causes issues. After calling this method, any saves stops working iOS7. Even on the PFUser. The PFUser has the installation setup and vice versa. An array of them.
Update 3: Seems like it's not just the addUniqueObject, but any setObject on the PFInstallation.currentInstallation. Help!
you should check your first param (isSuccess) as well:
if (bool == YES && error != nil) -> success
else -> failure
It took me a long while to realise, and even though I never actually found a solution to the problem itself, I found a workaround. I was saving the currentUser in the currentInstallation and the currentInstallation in the currentUser. This was causing issues when saving. I also saved channels on the currentInstallation by sending an array of channels directly instead of using addUniqueObject.
let installation = PFInstallation.currentInstallation()
installation.channels = channels;
installation.saveInBackground()

Use Parse.com Data in iOS 8 Notification Center widget

I am trying to download a few strings from parse.com inside of a Notification Center widget.
Therefore I first need to sign the user in to parse. First I am loading the credentials, which are saved from the app itself in the NSUserDefaults (I know about the safety aspect.). This step works fine, but when I am performing the following code, the widget says, that the loading of data is not possible.
func signUserIn(username: String, password: String) {
PFUser.logInWithUsernameInBackground(username, password:password) {
(user: PFUser!, error: NSError!) -> Void in
if (user != nil) {
SMKeychainService.saveToken(password)
var defaults = NSUserDefaults(suiteName: "group.xxx.xxx")
defaults.setObject(username, forKey: "UsernameKey")
defaults.synchronize()
self.delegate?.signedIn!(true)
} else {
self.delegate?.signedIn!(false)
}
}
}
Has anyone of you tried to load data from parse.com inside of an widget and how did you do it. I canĀ“t even load the data, because like I mentioned the widget crashes at the sign in of the user.
The app itself has no problems with signing in to parse and loading the data.
I solved the problem
I did not implemented Parse.setApplicationId("xxx", clientKey: "xxx"), now I implemented it in viewDidLoad and the code works!
Setting the
Parse.setApplicationId(appID, clientKey: clientKey)
in the AppDelegate does not work for the extension. Use this code in the viewDidLoad of the extension and it works fine.

Resources