简体   繁体   中英

How to force asynchronously save a constant subclass?

Edit 1: I've restructured my ViewControllers to make it easier to get what I want done.

Edit 2: I realized something major was missing while adding notes to my code, another function overrides the first segue.

This ViewController is where the annotation is created; all I need from this view is for the touchMapCoordinates to be transferred to the other ViewController so I can save the PFGeoPoint in an array.

Edit 3 After long work on understanding what is going on and simplifying the code, i've came down to the final conclusion based off of Swift- variable not initialized before use (but it's not used) , that the current method that I'm trying to use will not work in any case or scenario due to it saving Asynchronously. If anyone knows a work around, then you have officially done something that hasn't been done before :).

Error that is showing up is

Constant 'boi' used before being initialized

Subclass that is declared in Appdata to be used anywhere within the project

import Foundation
import Parse
import MapKit

class MyAnnotation: PFObject, PFSubclassing, MKAnnotation {

// MARK: - Properties

@NSManaged var location: PFGeoPoint

// MARK: - Initializers

init(coordinate: CLLocationCoordinate2D) {
    super.init()
    self.location = PFGeoPoint(latitude: coordinate.latitude, longitude: coordinate.longitude)
    print(location)

}

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()


    }
}

// MARK: - PFSubclassing protocol

static func parseClassName() -> String {
    return "AnnotationPins"
}

// MARK: - MKAnnotation protocol

var coordinate: CLLocationCoordinate2D {
    return CLLocationCoordinate2DMake(location.latitude, location.longitude)
}

var title: String? = "Start Topic"

}

Where the code will all be saved asynchronously together

   } else {

        let imageData = UIImagePNGRepresentation(self.galleryCameraImage.image!)
        let parseImageFile = PFFile(name: "upload_image.png", data: imageData!)
        let boi : MyAnnotation

        let textTitleandText = PFObject(className: "AnnotationPins")
        textTitleandText["textTopic"] = userTopic.text
        textTitleandText["textInformation"] = userText.text
        textTitleandText["userUploader"] = PFUser.currentUser()
        textTitleandText["imageFile"] = parseImageFile!
        textTitleandText["location"] = boi.location

        textTitleandText.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
            if error == nil {

If anyone could help it would be really appreciated!

Lets treat your Location data as a normal data to be transferred through segues. You can use this method to configure your destination View controller variable(same type) that will hold your location data.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   //check your segue name
   if segue.identifier == "YourSegueIdentifier" {
       let destinationVC = segue.destinationViewController as! YourDestinationViewController
       destinationVC.locationVariableInDestinationVC = locationVariableInCurrentVC 
   }

}

Above is the simplest way to pass data via segue, you can use the same approach for your location data too.

Hope that helps!!

Update: Based on your updated code

Move func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {} out of your handleLongPress function. PrepareForSegue function gets called automatically when there is any navigation happening through segues..

If you want to initiate a segue navigation programatically then assign a identifier to the segue and just call self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)

Over ride prepareForSegue method like below.

override func prepareForSegue(segue: UIStoryboardSegue, sender: 

AnyObject?) {
       if segue.identifier == "SegueID" {
           let destinationVC = segue.destinationViewController as! DestinationViewController  
// Create property in destinationView controller & assign required data from here.
       }
    }

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM