如何减少在Android上发布从服务器获取的数据的延迟?

时间:2015-09-02 15:59:30

标签: java android firebase

我遇到的问题是从Firebase获取一些数据之间有一点延迟,并且它是“可见的”#34;在板上。

基本上,通过createBoard()方法在电路板上放置一些数据后,它会立即显示在电路板上。但是,如果我从SomeActivity.java转换 - > AnotherActivity.java - > SomeActivity.java,从服务器获取数据有一个小延迟,最后它在板上填充到可见的位置。

从AnotherActivity.java转换回SomeActivity.java后,电路板空白1秒以上,然后数据显示在电路板上。

SomeActivity.java:

public class SomeActivity extends AppCompatActivity
{
    TouchImageView imageView;

    Button postBtn;

    private List<TextView> board;
    private List<Pin> boardObjs;

    TextView boardTextView;

    FrameLayout frame;

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

        board = new ArrayList<TextView>();
        boardObjs= new ArrayList<>();

        Firebase.setAndroidContext(this);
        database = new Firebase("https://"+board+".firebaseio.com/");

        database.child(time).addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(DataSnapshot snapshot)
            { 
                for (DataSnapshot postShot : snapshot.getChildren())
                {
                    Board posting = postShot.getValue(Board.class);
                    boardObjs.add(posting);
                }

                postOnBoard();

            }

            @Override
            public void onCancelled(FirebaseError firebaseError)
            {

            }
        });
    }

    private void postOnBoard()
    {
        for(Board b : boardObj)
        {
            boardTextView = new TextView(getApplicationContext());
            boardTextView.setLayoutParams(new LayoutParams(150, 100));

            pin.setBackgroundDrawable(getResources().getDrawable(b.color));
            frame.addView(boardTextView );
            boardTextView.setX(b.xLoc);
            boardTextView.setY(b.yLoc);

            boardTextView.setOnTouchListener(new View.OnTouchListener()
            {
                TextView tempBoardTextView = boardTextView;

                @Override
                public boolean onTouch(View view, MotionEvent e)
                {
                    AlertDialog contentsBox;
                    contentsBox = new AlertDialog.Builder(SomeActivity.this).create();
                    TextView v = (TextView) view;
                    String contents = v.getText().toString();
                    contentsBox.setMessage(contents);
                    contentsBox.setButton(AlertDialog.BUTTON_NEUTRAL, "Done",
                            new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });

                    View v2 = getLayoutInflater().inflate(R.layout.dialog_geisel, null);
                    contentsBox.setCustomTitle(v2); 

                    switch (e.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                        {

                            imageView.clickedFromBoard = true;
                            imageView.boardClicked = contentsBox;
                            break;
                        }

                        case MotionEvent.ACTION_MOVE:
                        {    
                            return false;
                        }

                        case MotionEvent.ACTION_UP:
                        {
                            return false;
                        }
                    }

                    return false;
                }
            });
            board.add(boardTextView);

            boardTextView.setVisibility(View.VISIBLE);
        }
        postBtn.setEnabled(true);
        postBtn.bringToFront();
    }

    public void createBoard()
    {
        // Do whatever to create the board and put visible data on it
    }
}

从AnotherActivity.java返回时,这是否可以补救到有直接可见数据的地方 - &gt; SomeActivity.java?

由于

1 个答案:

答案 0 :(得分:2)

ValueEventListener附加到数据库中的某个位置后,Firebase会开始下载数据。完全下载数据后,它会调用您的onDataChange方法。根据您下载的数据量以及连接的延迟和吞吐量,这可能需要一些时间。显然在你的情况下,这是大约一秒钟。

您可以采取一些措施来解决这种延迟:

  1. load less data

  2. 在数据加载时显示一些动画

  3. pre-fetch the datakeep it on disk

  4. 前两个应该相当简单。最后一个是几个月前引入Firebase原生移动SDK的new feature