Set values of a PFObject array to a Swift array - xcode

(I'm new to programming and totally new to Parse, so simplified explanations are certainly appreciated)
I imagine this is pretty straightforward; I'm just stuck. I have a PFObject saved in Parse.com that contains an array containing strings. I'm trying to set an array in my Swift app with the values in the Parse array.
var query = PFQuery(className:"ParseHat")
query.getObjectInBackgroundWithId("xxxxxxxxxx")
submittedNames = query["theHat"] as [String]
// submittedNames is declared elsewhere in the code. "theHat" is the key where
the array is stored in the PFobject
I get the error 'PFQuery' does not have a member named 'subscript'. I've tried doing a few things I didn't fully understand to the code but have gotten other errors so I'm just posting this as it seems to most closely resemble the method for retrieving objects in the Parse docs.

Try this out...
var query = PFQuery(className: "ParseHat")
query.getObjectInBackgroundWithId("XXXXXXXXX") {
(objects: PFObject?, error: NSError?) -> Void in
if error == nil {
//fetching users
for object in objects{ //looping through returned data
self.resultsArray.append(object.objectForKey("XXXXXXXX") as! String)
//adding the string to var resultsArray = [String]()
}
} else {
println("error :(")
}
}

Related

I'm trying to delete a record out of Core Data in xCode 8/Swift 3 & latest core data syntax

I'm trying to delete an entire record out of coreData. I've retrieved the data and placed it in an array for manipulation (I have another function that lets the user edit the data using this method and it works fine) But I can't figure out how to just delete the record. [.remove(at: index)] doesn't work and neither does the code below. I can set all the fields to empty but that's not what I want, I want the record gone completely.
I went through the solutions given for similar problems but to no avail
#IBAction func Delete(_ sender: UIButton) { // The delete function
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "DestinationsOne")
let context = appDelagate.persistentContainer.viewContext
var destArray = [DestinationsOne]() // The data array
do {
try destArray = context.fetch(request) as! [DestinationsOne]} //Fetching the data and placing it in the array
catch{
//error message
}
for index in (0..<destArray.count - 1){ //Go through the records
if destArray[index].destID == IDTitle!{ //Picks the record to edit
let object = destArray[index]
context.delete(object
}
appDelagate.saveContext()
}
I figured this one out. I'm posting the solution in case anyone else has the same question
func deleteRecords() -> Void { //The function to delete the record
let moc = getContext()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "DestinationsOne")
let result = try? moc.fetch(fetchRequest)
let resultdata = result as! [DestinationsOne] // result as entity
for object in resultdata { // Go through the fetched result
if object.destID == self.IDTitle{ // If there is a match
moc.delete(object) // delete the object
}
}
do {
try moc.save() // Save the delete action
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
func getContext () -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
deleteRecords() // Call the function
Why not applying a predicate to search this particular record. It's much more efficient than looping through a huge list.
func deleteRecords() { //The function to delete the record
let moc = getContext()
let fetchRequest = NSFetchRequest<DestinationsOne>(entityName: "DestinationsOne")
let predicate = NSPredicate(format: "destID == %#", self.IDTitle)
fetchRequest.predicate = predicate
do {
let resultdata = try moc.fetch(fetchRequest) // no type cast needed
if let objectToDelete = resultdata.first {
moc.delete(objectToDelete) // delete the object
try moc.save() // Save the delete action
}
} catch {
print("Could not save error: ", error)
}
}
Here are some issues with your code:
viewContext should be treated as readonly - you should use performBackgroundTask for all changes to core-data
You are fetching ALL of the entities and then then going through each one to find the one you want to delete. It is a lot faster to have core-data only fetch the one you want. You can do this by setting a predicate to the fetch request.
Instead of displaying your records by doing a fetch and using the array as a model, it is better to use a NSFetchedResultsController to do the fetch and manage the results. The fetchedResultsController will keep the data in sync when objects are changed, inserted or deleted. It also has delegate methods that will inform you when there are changes so you can update your view.
remove appDelagate.saveContext from your project. Apple's template code is wrong. You should never be writing to the viewContext so you should never have a reason to save it.
where is IDTitle being set? are you sure it is not nil?
(minor) for index in (0..<destArray.count - 1){ can be replaced with for (index, element) in destArray.enumerated() { which is clearer to read.

Passing Dictionary to Watch

I'm trying to pass data from iPhone -> Watch via Watch Connectivity using background transfer via Application Context method.
iPhone TableViewController
private func configureWCSession() {
session?.delegate = self;
session?.activateSession()
print("Configured WC Session")
}
func getParsePassData () {
let gmtTime = NSDate()
// Query Parse
let query = PFQuery(className: "data")
query.whereKey("dateGame", greaterThanOrEqualTo: gmtTime)
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objectsFromParse = objects as? [PFObject]{
for MatchupObject in objectsFromParse
{
let matchupDict = ["matchupSaved" : MatchupObject]
do {
try self.session?.updateApplicationContext(matchupDict)
print("getParsePassData iPhone")
} catch {
print("error")
}
}
}
}
}
}
I'm getting error twice printed in the log (I have two matchups in Parse so maybe it knows there's two objects and thats why its throwing two errors too?):
Configured WC Session
error
error
So I haven't even gotten to the point where I can print it in the Watch app to see if the matchups passed correctly.
Watch InterfaceController:
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
let matchupWatch = applicationContext["matchupSaved"] as? String
print("Matchups: %#", matchupWatch)
}
Any ideas? Will post any extra code that you need. Thanks!
EDIT 1:
Per EridB answer, I tried adding encoding into getParsePassData
func getParsePassData () {
let gmtTime = NSDate()
// Query Parse
let query = PFQuery(className: "data")
query.whereKey("dateGame", greaterThanOrEqualTo: gmtTime)
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objectsFromParse = objects as? [PFObject]{
for MatchupObject in objectsFromParse
{
let data = NSKeyedArchiver.archivedDataWithRootObject(MatchupObject)
let matchupDict = ["matchupSaved" : data]
do {
try self.session?.updateApplicationContext(matchupDict)
print("getParsePassData iPhone")
} catch {
print("error")
}
}
}
}
}
}
But get this in the log:
-[PFObject encodeWithCoder:]: unrecognized selector sent to instance 0x7fbe80d43f30
*** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
EDIT 2:
Per EridB answer, I also tried just pasting the function into my code:
func sendObjectToWatch(object: NSObject) {
//Archiving
let data = NSKeyedArchiver.archivedDataWithRootObject(MatchupObject)
//Putting it in the dictionary
let matchupDict = ["matchupSaved" : data]
//Send the matchupDict via WCSession
self.session?.updateApplicationContext(matchupDict)
}
But get this error on the first line of the function:
"Use of unresolved identifer MatchupObject"
I'm sure I must not be understanding how to use EridB's answer correctly.
EDIT 3:
NSCoder methods:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
//super.init(coder: aDecoder)
configureWCSession()
// Configure the PFQueryTableView
self.parseClassName = "data"
self.textKey = "matchup"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
Error
You are getting that error, because you are putting a NSObject (MatchupObject) which does not conform to NSCoding inside the dictionary that you are going to pass.
From Apple Docs
For most types of transfers, you provide an NSDictionary object with
the data you want to send. The keys and values of your dictionary must
all be property list types, because the data must be serialized and
sent wirelessly. (If you need to include types that are not property
list types, package them in an NSData object or write them to a file
before sending them.) In addition, the dictionaries you send should be
compact and contain only the data you really need. Keeping your
dictionaries small ensures that they are transmitted quickly and do
not consume too much power on both devices.
Details
You need to archive your NSObject's to NSData and then put it in the NSDictionary. If you archive a NSObject which does not conform to NSCoding, the NSData will be nil.
This example greatly shows how to conform a NSObject to NSCoding, and if you implement these things then you just follow the code below:
//Send the dictionary to the watch
func sendObjectToWatch(object: NSObject) {
//Archiving
let data = NSKeyedArchiver.archivedDataWithRootObject(MatchupObject)
//Putting it in the dictionary
let matchupDict = ["matchupSaved" : data]
//Send the matchupDict via WCSession
self.session?.updateApplicationContext(matchupDict)
}
//When receiving object from the other side unarchive it and get the object back
func objectFromData(dictionary: NSDictionary) -> MatchupObject {
//Load the archived object from received dictionary
let data = dictionary["matchupSaved"]
//Deserialize data to MatchupObject
let matchUpObject = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! MatchupObject
return matchUpObject
}
Since you are using Parse, modifying an object maybe cannot be done (I haven't used Parse in a while, so IDK for sure), but from their forum I found this question: https://parse.com/questions/is-there-a-way-to-serialize-a-parse-object-to-a-plain-string-or-a-json-string which can help you solve this problem easier than it looks above :)

Fetching Pointers In Parse

I have a class in Parse called "WorkoutExercise" with pointers to other objects and i am trying to retrieve the workout exercises with the pointed objects as part of the objects too.
But when i execute the following code, none of the pointed objects show up in the workoutexercise object.
I tried includeKey("Exercise"), but that does not seem to work. Any clues?
Code:
// Return pfobjects casted as exercises for a given workout
func getExercisesForWorkout(workout:Workout!, completion: ([WorkoutExercise]!, NSError!) -> Void ){
var query = PFQuery(className:"WorkoutExercise")
query.orderByAscending ("sequence")
query.whereKey("Workout", equalTo: workout)
query.includeKey("Exercise")
query.includeKey("Workout")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
completion(objects as? [WorkoutExercise], error)
}
}
In Parse:
Result:
[<WorkoutExercise: 0x7feef1c57700, objectId: Q9KeD4WiuH, localId: (null)> {
Workout = "<Workout: 0x7feef1e89150, objectId: 1PQCrl3f8L>";
reps = 10;
sequence = 1;
weight = 20;
}]
Ok, i know the mistake.
You are not supposed to name your pointers with the uppercase.
Changed the pointers to lower case and all is well.

casting arrays returned by NSURL.getResourceValue in Swift

I have spent all day trying to get useable results from NSURL.getResourceValue for NSURLTagNamesKey in swift. The function should take the path name as a string and return an array of strings for the user tags. I have a version of this that works in Objective C, but have not been able to re-write in Swift.
This is the current version of the code:
func listTags(filePath:String)->[String]{
//convert path string to NSURL
let theURL : NSURL = NSURL.fileURLWithPath(filePath)!
//get tags for NSURL -- should be NSArray of NSStrings hiding in an AnyObject?
var tags : AnyObject?
var anyError: NSError?
tags = theURL.getResourceValue(&tags, forKey:NSURLTagNamesKey, error: &anyError)
//unwrap tags object? This part never works
let tagArray = tags! as [AnyObject]
//insert every item in tag array into results array as a String
var results = [String]()
for object in tagArray{
results.append(object as String)
}
return results
}
The code will compile but breaks when it tries to convert the AnyObject to any other type. I have tried every combination I can think of -- [AnyObject], [String], NSArray, with/without exclamation points and question marks.
Am on verge of giving up on Swift.
You're going to kick yourself...
The method getResourceValue:forKey:error returns a value - a Bool, indicating whether the container you passed in as the first argument has been populated. Unfortunately you're assigning the value of this boolean to tags - your container! - which means that whatever was passed in to this container by Cocoa is immediately over-written. This worked for me...
var tags : AnyObject?
var anyError: NSError?
var success = theURL.getResourceValue(&tags,
forKey:NSURLTagNamesKey,
error: &anyError)
if success {
println("container contents \(tags as [String])") // -> [AutoLayout, Swift]
}
With Swift 2, getResourceValue(:forKey:) returns () throws, i.e., void type which throws errors, so the answer above will no longer work. It needs to be wrapped in a do {try} catch{} construction without the anyError variable:
do {
try theURL.getResourceValue(&tags, forKey: NSURLTagNamesKey)
return tags as! [String]
} catch {
// process the error here
}

Parse Framework with Swift

Has anyone tried using the Parse Framework with swift yet? As long as you add a bridge-file you can work with both swift and objective-c code.
Here is my query.. the 'objects' array returned from Parse has all my data properly, but the method is returning before setting the 'results' array to the 'objects' array, so i keep getting nothing back from the function. Perhaps Parse will need to receive an update to support swift, or did I possibly make a mistake somewhere? Thanks
class func fetchAllCategories() -> NSArray {
var results : NSArray = NSArray()
var query : PFQuery = PFQuery(className: "Category")
query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in
if (error != nil) {
NSLog("error " + error.localizedDescription)
}
else {
NSLog("objects %#", objects as NSArray)
results = NSArray(array: objects)
}
})
NSLog("results %#", results)
return results
}
This has nothing to do with Swift. query.findObjectsInBackgroundWithBlock does the work in the background so it's going to finish much later, after the function returns.

Resources