iosparse-platformswift

How to make this Parse.com cloud code call?


Here's an iOS7 Parse cloud code call ...

How to do this in SWIFT ?

To be clear ... can you use "callFunctionInBackground" in SWIFT, or do you have to just call to an objc class?

-(void)tableView:(UITableView *)tableView
        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
        forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    int thisRow = indexPath.row;
    PFUser *delFriend = [self.theFriends objectAtIndex:thisRow];
    
    NSLog(@"you wish to delete .. %@", [delFriend fullName] );
    
    // note, this cloud call is happily is set and forget
    // there's no return either way. life's like that sometimes
    
    [PFCloud callFunctionInBackground:@"clientRequestFriendRemove"
            withParameters:@{
                            @"removeThisFriendId":delFriend.objectId
                            }
            block:^(NSString *serverResult, NSError *error)
            {
            if (!error)
                {
                NSLog(@"ok, Return (string) %@", serverResult);
                }
            }];
    
    [self back];    // that simple
    }

Note

Here's some thoughts from CA at Parse about this:

https://www.parse.com/questions/ios-when-will-swift-for-parse-be-ready

(Notice how popular that question is - hot topic).

Update

Note, I've noticed this is a Google landing page for trying to figure out "how the heck to do cloud code calls" (unrelated to Swift). Here is a full, complete set of example code for both iOS and Android of custom cloud code functions, in the Parse.com universe https://stackoverflow.com/a/24010828/294884


Solution

  • Add an Objective-C .m file to your project. Xcode will ask about creating a Bridge Header file. Say yes.

    Delete the .m file. In the bridge header file, add your import statement:

    #import <Parse/Parse.h>
    

    Another answer, which has a way better walkthrough than mine: https://stackoverflow.com/a/24005242/353988

    Now you can call native Parse code in Swift, i.e.:

    import UIKit
    import Foundation
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
      var window: UIWindow?
    
      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    
        Parse.setApplicationId("appid", clientKey: "clientkey")
        var obj = PFObject(className:"TestObject")
        obj.setObject("bar", forKey: "foo")
        obj.saveInBackgroundWithBlock ({ 
          (succeeded: Bool!, err: NSError!) -> Void in
          NSLog("Hi")
        })
    
      }
    
    }