Objective C to Swift - DoWithBlock

末鹿安然 提交于 2019-12-12 02:23:55

问题


I am trying to only create an array in Swift when an objective C class function is completed. I am using a DoWithBlock in the objective C function but I am am new to IOS and am trying to work out if it is actually running in the correct order.

I got help to come up the solution on here but I am not sure if what I did to extend on that answer is correct and I believe it should be a new question.

In the objective C class I am calling the function with the block like this:

- (void)scanTableDoWithBlock:(void(^)(NSArray *scanResult, NSError *error))handler {

    AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
    AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
    scanExpression.limit = @10;


    [[dynamoDBObjectMapper scan:[Mapper class]
                     expression:scanExpression]
     continueWithBlock:^id(AWSTask *task) {
         if (task.error) {
             NSLog(@"The request failed. Error: [%@]", task.error);
             if (handler != nil) {
                 handler(nil, task.error);
             }
         }
         if (task.exception) {
             NSLog(@"The request failed. Exception: [%@]", task.exception);
         }
         if (task.result) {
             AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
             NSMutableArray *scanResult = [[NSMutableArray alloc] initWithArray:paginatedOutput.items];  //// ADDED /////

             if (handler != nil) {
                 handler([scanResult copy], nil);
             }
         }

         return nil;
     }];    
}
@end

Then in the swift class I am calling the objective C function and I am hoping creating the array like this:

override func viewDidLoad() {
    super.viewDidLoad()

    let scanTable = ScanTable();

    scanTable.scanTableDoWithBlock { (scanResult, error) in

         let swiftArray = scanTable.scanResult
    }

Does the code "let swiftArray = scanTable.scanResult" only run when the objective C function has completed or would it be just pure luck if it ran before the other. I haven't been able to find good documentation on using blocks in swift.

Thanks for your help

/// Edit My latest attempt at calling the objective C scan function in swift viewwilappear (Within the scan function the array is being created and I can for loop through it fine. On on the swift side I am getting both arrays return as nill the code is running and ////

class RateSongsViewController: UIViewController {

    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)


        let scanTable = ScanTable();

        scanTable.scanTableDoWithBlock { (scanResult, error) in

            let swiftArray = scanTable.scanResult


            if (scanTable.scanResult == nil){

                print(" scanResult ARRAY IS NILL")

            } else {
                print(" scanResult ARRAY IS NOT NILL")
            }

            if (swiftArray == nil){

                 print(" SWIFT ARRAY IS NILL")

            } else {
                 print(" SWIFT ARRAY IS NOT NILL")
            }

        }

    }

回答1:


  1. Don't call scanTable.scanTableDoWithBlock in viewDidLoad; if its heavy computation on MainThread it will delay the view transition. Call it in viewDidAppear.
  2. let swiftArray = scanTable.scanResult will call after scanTableDoWithBlock completion.

  3. If task.exception you should call

    if (handler != nil) {
        handler(nil, nil);
    }
    

Remove return nil because it will not call completion block.



来源:https://stackoverflow.com/questions/38847930/objective-c-to-swift-dowithblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!