可观察的fork join问题

时间:2017-05-22 11:55:43

标签: sqlite angular reactjs observable ionic3

嗨我有3张桌子,每张桌子都是另一张桌子的孩子。我写了一个从sqllite db中获取的方法,如下所示

public downloadFromOfflineDB(db,testSO){
           var observableBatch = [];
           observableBatch.push(db.executeSql("select * from TMP_AUD WHERE CRE_BY=? AND AUD_NUMBER=? ",
                        [localStorage.getItem("user_name"), testSO.auditNumber]).then(
                        response => {
                            this._util.logData('In downloadPendingInstancesForSyncFromOfflineDB- folder'+response.rows.length+'ID= '+response.rows.item(0).FLD_NUMBER);
                            if (response && response.rows && response.rows.length > 0) {
                                if (response && response.rows && response.rows.length > 0) {
                                    var FLD_NUMBER = response.rows.item(0).FLD_NUMBER;
                                    var folderArray = []
                                    observableBatch.push(db.executeSql("select * from TMP_FOLDER WHERE CRE_BY=? AND FLD_NUMBER=? ",
                                        [localStorage.getItem("user_name"), FLD_NUMBER]).then(
                                        a => {
                                              this._util.logData('In downloadPendingInstancesForSyncFromOfflineDB-TMP_FOLDER'+a.rows.length);
                                            if (a && a.rows && a.rows.length > 0) {
                                                for (let i = 0; i < a.rows.length; i++) {
                                                    var folderObj = {
                                                        folderName: a.rows.item(i).FLD_NAME,
                                                        files:[]
                                                    }

                                                    var FLD_NAME = a.rows.item(i).FLD_NAME
                                                   this._util.logData('In downloadPendingInstancesForSyncFromOfflineDB-TMP_FOLDER '+FLD_NAME);
                                                   observableBatch.push( db.executeSql("select * from TMP_FILES WHERE CRE_BY=? AND FLD_NAME=? ",
                                                        [localStorage.getItem("user_name"), FLD_NAME]).then(
                                                        b => {
                                                              this._util.logData('In downloadPendingInstancesForSyncFromOfflineDB-TMP_FILES'+b.rows.length);
                                                            var fileArray = [];
                                                            if (b && b.rows && b.rows.length > 0) {
                                                                for (let j = 0; j < b.rows.length; j++) {
                                                                    var fileSO = {
                                                                        compliance: b.rows.item(j).COMPLIANCE,
                                                                        remarks: b.rows.item(j).REMARKS,
                                                                        fileName: b.rows.item(j).FILE_NAME,
                                                                        title: b.rows.item(j).TITLE

                                                                        }

                                                                    );
                                                                   fileArray.push(fileSO);
                                                                }}
                                                             folderObj.files=fileArray;
                                                            }).catch(
                                                                            e => {
                                                                                this._util.logData('For sync error'+JSON.stringify(e));
                                                                                return Observable.throw("An error occurred during sync");
                                                                            })
                                                            );

                                                        folderArray.push(folderObj);
                                                }}

                                        }).catch(
                                                    e => {
                                                        this._util.logData('For sync error'+JSON.stringify(e));
                                                        return Observable.throw("An error occurred during sync");
                                                    })
                                        );
                                }


                            }
                            testSO.folderArray = folderArray;
                            this._util.logData('Candidate for selected for sync' + JSON.stringify(testSO));

                        })
           );
        return Observable.forkJoin(observableBatch);
    }

这里的问题是下面的方法是不等待所有的调用完成

 public getFiles(testSO) {

        return Observable.create(observer => {

            this.platform.ready().then(() => {
                this.sqlite.create({
                    name: 'offline.db',
                    location: 'default'
                }).then((db: SQLiteObject) => {
                    this.downloadFromOfflineDB(db, testSO).subscribe(c => {
                        observer.next(c[0]);//This is undefined
                        observer.complete();
                    },
                    error => {

                        observer.error("An error occurred sync files.");                                                                                        
                    });

                 });
            });
        });
    }

第一个方法正在执行,而第二个方法在第一个执行完成之前返回,而我没有填充对象testSO。有人可以指导我,并告诉我我在这里做错了什么。我使用了可观察的叉加入。

1 个答案:

答案 0 :(得分:0)

您似乎只使用一个项目调用了<?php // Put your device token here (without spaces): $deviceToken = '1234567890123456789'; // // Put your private key's passphrase here: $passphrase = 'ProjectName'; // Put your alert message here: $message = 'My first silent push notification!'; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'PemFileName.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server $fp = stream_socket_client( // 'ssl://gateway.push.apple.com:2195', $err, 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array( 'content-available'=> 1, 'alert' => $message, 'sound' => 'default', 'badge' => 0, ); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); - 结果为$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem // Convert .p12 to .pem. Enter your pass pharse which is the same pwd that you have given while creating the .p12 certificate. PEM pass phrase also same as .p12 cert. $ openssl pkcs12 -nocerts -out PushKey1.pem -in pushkey.p12 Enter Import Password: MAC verified OK Enter PEM pass phrase: Verifying - Enter PEM pass phrase: // To remove passpharse for the key to access globally. This only solved my stream_socket_client() & certificate capath warnings. $ openssl rsa -in PushKey1.pem -out PushKey1_Rmv.pem Enter pass phrase for PushChatKey1.pem: writing RSA key // To join the two .pem file into one file: $ cat PushCert.pem PushKey1_Rmv.pem > ApnsDev.pem 。如果您稍后添加更多项目,则不会影响Observable.forkJoin(observableBatch)