函数返回null值

时间:2017-07-09 22:37:47

标签: swift function return-value

这可能是一个基本问题,但我无法弄清楚这一点。我的函数总是返回一个空值,我不知道为什么。

我的主代码(在不同的视图控制器中)使用"字符串"来调用此函数。寻找。但在功能结束时,我永远无法返回结果。如果我打印搜索结果,我有最终的打印声明,那么我确实看到了结果。但是在该函数完成之后,返回值为零。如何从此函数获取返回值?提前致谢,。我正在使用Swift btw。

EditText phoneNum,Code;// two edit text one for enter phone number other for enter OTP code
Button sent_,Verify;  // sent button to request for verification and verify is for to verify code
private PhoneAuthProvider.ForceResendingToken mResendToken;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private FirebaseAuth mAuth;
private String mVerificationId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_phone_number_auth);// layout 


    phoneNum =(EditText) findViewById(R.id.fn_num);
    Code =(EditText) findViewById(R.id.code);

    sent_ =(Button)findViewById(R.id.sent_nu);
    Verify =(Button)findViewById(R.id.verify);

    callback_verificvation();                   ///function initialization

    mAuth = FirebaseAuth.getInstance();


    sent_.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String num=phoneNum.getText().toString();
            startPhoneNumberVerification(num);          // call function for receive OTP 6 digit code
        }
    });
    Verify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String code=Code.getText().toString();
            verifyPhoneNumberWithCode(mVerificationId,code);     //call function for verify code 

        }
    });
}

private void startPhoneNumberVerification(String phoneNumber) {
    // [START start_phone_auth]
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks
    // [END start_phone_auth]


}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information

                        FirebaseUser user = task.getResult().getUser();
                        Toast.makeText(getApplicationContext(), "sign in successfull", Toast.LENGTH_SHORT).show();
                        // [START_EXCLUDE]


                    } else {
                        // Sign in failed, display a message and update the UI

                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid

                        }

                    }
                }
            });
}
private void verifyPhoneNumberWithCode(String verificationId, String code) {
    // [START verify_with_code]
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
    // [END verify_with_code]
    signInWithPhoneAuthCredential(credential);
}


private void callback_verificvation() {

    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verificaiton without
            //     user action.
            // [START_EXCLUDE silent]


            // [START_EXCLUDE silent]

            signInWithPhoneAuthCredential(credential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            // [START_EXCLUDE silent]


            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request


                // [END_EXCLUDE]
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded

            }


        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.


            // Save verification ID and resending token so we can use them later
            mVerificationId = verificationId;
            mResendToken = token;


        }
    };

//你可以忽略以下所有这些,直到最后几行......

 func fetchSearchResults (_ searchString: String) -> Array<Any> {
        self.globalPost = [Posts]()
        self.postKeysArray = []
        var searchIndex:AlgoliaSearch.Index
        let query = Query()
        searchIndex = self.client.index(withName: "Comments")

//所以我的搜索代码在这里返回一些值,我在post keysArray中看到它们。

        query.attributesToRetrieve = ["userComment","postId"]
        query.attributesToHighlight = ["postId"]
        query.query = searchString
        query.optionalWords = [searchString]

        let curSearchId = searchId

        searchIndex.search(query, completionHandler: { (content, error) in
            if (curSearchId <= self.displayedSearchId) || (error != nil) {
                return // Newest query already displayed or error
            }
            self.displayedSearchId = curSearchId
            self.loadedPage = 0 // Reset loaded page

            guard let hits = content!["hits"] as? [[String: AnyObject]] else { return }
            var tmp = [String]()

            for hit in hits {
                //                print ("Comment is \(commentRecord(json:hit).postId)")
                tmp.append(commentRecord(json:hit).postId!)
            }
            self.postKeysArray = tmp
            self.getDatafromFirebase(searchString)

//但在这里我得到一个NULL值。

            print ("Search Results -post keys is \(self.postKeysArray)")  \\ returns correct values here in the Array
        })
        self.searchId += 1

1 个答案:

答案 0 :(得分:0)

正如Alexander所说,您正在完成处理程序中打印搜索结果,该处理程序在fetchSearchResults返回时尚未执行。有几种不同的方法可以解决这个问题。一个是重构fetchSearchResults来进行封闭:

func fetchSearchResults (_ searchString: String, completionHandler completion: (Array<Any>) -> ()) {
    // ...
    searchIndex.search(query, completionHandler: { (content, error) in
        // ...
        self.postKeysArray = tmp
        self.getDatafromFirebase(searchString)
        // so my search code returns some values here and i see them in the post keysArray.

        print ("Search Results -post keys is \(self.postKeysArray)")  \\ returns correct values here in the Array
        completion(self.postKeysArray)
    })
}

然后,在您的“主要代码”中,无论您在哪里打电话:

let results = searchController.fetchSearchResults(searchString)
doSomething(with: results)

你现在有:

searchController.fetchSearchResults(searchString, { results in
    doSomething(results)
})

这一行也可以写成:

searchController.fetchSearchResults(searchString, doSomething)

如果你真的不能让fetchSearchResults成为异步,那么你可以使用DispatchSemaphore等待完成处理程序完成:

func fetchSearchResults (_ searchString: String) -> Array<Any> {
    // ...
    var semaphore = DispatchSemaphore(0)
    searchIndex.search(query, completionHandler: { (content, error) in
        // ...
        self.postKeysArray = tmp
        self.getDatafromFirebase(searchString)
        // so my search code returns some values here and i see them in the post keysArray.

        print ("Search Results -post keys is \(self.postKeysArray)")  \\ returns correct values here in the Array
        semaphore.signal()
    })
    semaphore.wait()
    return self.postKeysArray
}

请注意,除非您知道自己在做什么,否则使用信号量是一个好主意。如果您不知道可能调用各种回调的线程,则可能导致死锁。如果在与searchIndex.search相同的DispatchQueue上调用fetchSearchResults完成处理程序,则上述示例将无效。如果你不明白我在说什么,你可能不应该这样做。使用异步回调几乎肯定会更好。