2017-02-27 5 views
0

サービスのonMessageReceivedで受信したすべてのメッセージをSQLite db内に保存します。FirebaseMessagingServiceはインテントサービスですか?

私はdbを開き、データを挿入してdbを閉じる予定でした。これは、onMessageReceivedへのすべての呼び出しがキューに入れられ、1つずつ実行されるため、インテントサービスでうまく動作します。

しかし、onMessageReceivedが複数のメッセージに対して同時に呼び出されると、別の呼び出しが書き込みを試みている間にDbの問題が発生し、問題が発生する可能性があります。

私はどのような行動を期待しているか誰にでも確認できます。

それは意図サービスではない場合、私はあなたがあなたの要求をキューに入れ、背景順で実行することができるLinkedBlockingDeque<>クラスを確認することができDBシングルトンパターンと同期ブロックで

+0

あなたはfirebaseのオフライン使用をチェックアウトしたことがありますか? –

+0

それはサービスです...しかし同時にメッセージを受け取りますか?私が見たことから、複数のメッセージが1つずつ受信されます。しかし、それがいつもそうであるかどうか私は確信していません。 – Kushan

+0

@JadavLalit firebase DBの永続性がFCMを助けてくれるとは思わない。それはあなたが参照しているものですか? – Kushan

答えて

0

を見ているかもしれません。鉱山のサンプルクラス以下
チェック -

public class Processor implements Runnable { 

private final LinkedBlockingDeque<Task> mTaskQueue = new LinkedBlockingDeque<Task>(); 
private boolean mExecuteTask = true; 

@Override 
public void run() { 
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 
    // You can put some end condition if needed 
    while (mExecuteTask) { 
     Task task; 
     try { 
      // Waits if necessary until an element becomes available 
      task = (Task) mTaskQueue.take(); 
     } catch (InterruptedException e) { 
      continue; // re-test the condition on the eclosing while 
     } 
     if (task != null) { 
      task.runnable.run(); 
     } 
    } 
} 

// Call this method to put Runnable in Queue. 
public synchronized void put(int id, Runnable runnable) { 
    try { 
     Task task = new Task(); 
     task.runnable = runnable; 
     task.id = id; // someUniqueId to avoid duplication 
     // Check Id of tasks already present in Queue 
     mTaskQueue.addLast(task); 
    } catch (IllegalStateException ie) { 
     throw new Error(ie); 
    } 
} 

private static class Task { 
    public Runnable runnable; 
    // Unique Id of task 
    public int id; 
} 

}

関連する問題