问题
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:
- Don't call
scanTable.scanTableDoWithBlock
inviewDidLoad
; if its heavy computation on MainThread it will delay the view transition. Call it inviewDidAppear
. let swiftArray = scanTable.scanResult
will call afterscanTableDoWithBlock
completion.If
task.exception
you should callif (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