wait() notify() wird nicht "entsperrt"

J

Jojo1992

Gast
Hallo, Ich habe hier bereits vor Monaten einmal Hilfe zu meiner App erhalten,
Ich habe nun eine Sache gefunden die ich einfach nicht zum laufen bekomme.

Eine do while Schleife lädt Daten aus einer SQLiteDatenbank
wenn der Eintrag vorhanden ist wird eine Funktion aufgerufen: save_game();

und direkt danach wir die Funktion gesperrt, und soll erst weiterlaufen wenn die save_game funktion zu Ende ist. Die savegame funktion hat Kontakt zum Server und deswegen dauert die Abfrage etwas. In der zwischenZeit läuft die do while Schleife einfach weiter und somit wird es 3-4 versucht zu speichern...--> DESWEGEN die Wait() notifyall() Sache.

Code:

Code:
do {
                    final Cursor checker = db.query(true, "user_overview", new String[] {"id",},null,null,null,null,null,null);
                    if(checker.moveToFirst() ) {  // moves the cursor to the first row in the result set...
                    boolean delete = true;
do {
                            if (checker.getString(0).toString().equals(save_game.getString(0).toString())) {
                                delete = false;
                                Log.e("Speichern", "Spiel ist erfolgreicht getestet");
                                try {
                                    Cursor cursor = db.query(
                                            true,
                                            "game",
                                            new String[] {
                                                    "nick",
                                                    "id",
                                                    "game_nr",
                                                    "d1",
                                                    "l1",
                                                    "d2",
                                                    "l2",
                                                    "d3",
                                                    "l3",
                                                    "coin_sp",
                                                    "coin_ge",
                                                    "ready",
                                            },
                                            "id = ?",
                                            new String[]{ save_game.getString(0).toString() },
                                            null,
                                            null,
                                            null,
                                            null
                                    );
                                    if( cursor.moveToFirst() ) {
                                        Log.e("Speichern", "Spieldaten geladen");
                                        if (cursor.getString(11).equals("1")){
                                            Log.e("Speichern", "Spieldaten müssen gespeichert werden");
                                            save_game();
                                            synchronized (notifier) {
                                                while(!notifier) {
                                                    System.out.println("Spiel noch nicht gespeichert, warten...");
                                                    notifier.wait();
                                                }
                                            }
                                            if (notifier) {
                                                check2=true;
                                                Log.e("Speichern", "erfolgreich");
                                            } else {
                                                mRefresh.setVisibility(View.VISIBLE);
                                                mProgressBar.setVisibility(View.INVISIBLE);
                                                Log.e("Speichern", "nicht erfolgreich");
                                            }
                                        } else {
                                            Log.e("Speichern", "Spiel nicht vollständig");
                                            check2 = true;
                                        }
                                    } else {
                                        Log.e("Fehler", "Spiel ist nicht vorhanden?");
                                        check2=true;
                                    }
                                } catch(Exception h) {
                                    Log.e("Fehler", "Fehler beim Abfagen ob Spiel bereits gespielt!", h);
                                    check2=true;
                                }
                            }
                        } while (checker.moveToNext());
if (delete) {
                            db.delete("game", "id="+save_game.getString(0).toString(), null);
                        }
                    }

                } while (save_game.moveToNext());

SAVEGAMEFUNKTION:
Code:
private void save_game() {
        JsonObject json = new JsonObject();
        json.addProperty("nick", nick);
        json.addProperty("game_id", game_id);
        json.addProperty("game_nr", game_nr);

        json.addProperty("d1", d1);
        json.addProperty("d2", d2);
        json.addProperty("d3", d3);
        json.addProperty("l1", l1);
        json.addProperty("l2", l2);
        json.addProperty("l3", l3);
        json.addProperty("coin_sp", coin_sp);
        json.addProperty("coin_ge", coin_ge);
        Ion.with(getApplicationContext())
                .load("*********")
                .setJsonObjectBody(json)
                .asJsonObject()

                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        try {
                            Gson gson = new Gson();
                            save get_data2 = gson.fromJson(result, save.class);
                            if (get_data2.erfolgreich.equals("TRUE")) {
                                Log.e("UNLOCKEN", "JETZT");
                                synchronized (notifier) {
                                    System.out.println("UNLOCK:"+System.currentTimeMillis());
                                    notifier = Boolean.valueOf(true);
                                    notifier.notifyAll();
                                }
                                db.delete("game", "id=" + game_id, null);
                            } else {
                                Log.e("UNLOCKEN", "JETZT2");
                                notifier = Boolean.valueOf(false);
                                synchronized (notifier) {
                                    synchronized (notifier) {
                                        System.out.println("UNLOCK:"+System.currentTimeMillis());
                                        notifier = Boolean.valueOf(true);
                                        notifier.notifyAll();
                                    }
                                }
                            }
                        } catch (Exception f) {
                            Log.e("Fehler", "Save", f);
                            mRefresh.setVisibility(View.VISIBLE);
                            mProgressBar.setVisibility(View.INVISIBLE);
                            mConnection.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }

Das Problem ist das die Funktion einfach nciht "entsperrt" wird, sondern einfach bei wait() bleibt.
 
Ich hab mir den Code jetzt nicht im Detail angeguckt, aber warum lässt du die save_game methode nicht synchron laufen?

normalerweise läuft die Anwendung ja erst weiter wenn save_game fertig ist.
Das ist eigentlich der Normalzustand.

Evtl musst du dann halt das gesamte speicher zeug ingesamt asynchron machen.
 

Ähnliche Themen

M
  • MikelKatzengreis
Antworten
10
Aufrufe
232
swa00
swa00
SaniMatthias
Antworten
19
Aufrufe
963
swa00
swa00
O
Antworten
15
Aufrufe
2.975
ORHUX
O
Zurück
Oben Unten