简体   繁体   English

无需让用户等待即可在android中加载大量数据的最佳方法

[英]Best way to load a large amount of data in android without making the user wait

I'm working in an application that read phone contacts and use them in my application (Call history, Favorite contacts and All contacts). 我正在读取电话联系人并在我的应用程序(通话记录,收藏夹联系人和所有联系人)中使用它们的应用程序中工作。

My UI consist of tab host control and user can swap between them, as I want my data to be shared across all my activities and also to be saved in only one place. 我的UI由选项卡宿主控件组成,用户可以在它们之间交换,因为我希望我的数据可以在所有活动中共享并且也只能保存在一个位置。

So I have created a singleton class called data controller, and when I open the application I show loading screen until all data loaded. 因此,我创建了一个称为数据控制器的单例类,当我打开应用程序时,我将显示加载屏幕,直到所有数据加载完毕。

The problem now that user is complaining because of waiting a lot of time about (1 minute) every time they open the application when he has a very large amount of contacts, so how can I optimize my code in a good way? 现在的问题是,由于用户在有大量联系人的情况下每次打开应用程序时都等待大量时间(大约1分钟),因此用户在抱怨,那么如何才能以一种好的方式优化代码?

EDIT 编辑

This is the method that I'm using to get all contacts: 这是我用来获取所有联系人的方法:

public static ArrayList<ContactInfo> getAllContactWithNumberAndNameAndPhoto(
            Context context, boolean starred) {

        ArrayList<ContactInfo> retList = new ArrayList<ContactInfo>();

        ContentResolver cr = context.getContentResolver();

        Cursor cur = null;
        if (starred == true) {
            cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    "starred=?", new String[] { "1" }, null);
        } else {

            cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
                    null, null);
        }
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {

                ContactInfo item = new ContactInfo();
                String id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Uri photo = PhoneUtils.getPhotoUriFromID(context, id);
                String starredValue = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts.STARRED));
                boolean isFav = false;
                if (starredValue.equals("1"))
                    isFav = true;

                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);
                    while (pCur.moveToNext()) {

                        String phoneNo = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        item.addPhone(removeCharactersFromPhoneNumber(phoneNo));
                    }
                    pCur.close();

                    if (photo != null) {

                        item.setPhoto(photo.toString());
                    }

                    item.setName(name);
                    item.setFavorite(isFav);
                    item.setRecent(false);

                    retList.add(item);
                }
            }
            cur.close();
        }

        return retList;
    }

Please let me know if I can optimize this method. 如果可以优化此方法,请告诉我。

Im surprised it takes that long to load the contacts from the device. 我很惊讶从设备加载触点要花费这么长时间。

Have you profiled the app to see where the time is actually spent? 您是否配置了应用程序的配置文件以查看实际花费的时间? Something seems wrong here. 这里似乎有些问题。

If it truly takes that long to load from the system providers (due to the OS) you could cache the results (ie put in your own SQL db) so you can load quickly on each app visit (< 1 sec) and refresh from device in the background. 如果确实需要很长的时间从系统提供程序加载(由于操作系统),则可以缓存结果(即放入您自己的SQL数据库中),这样就可以在每次应用访问时快速加载(<1秒)并从设备刷新在后台。

I guess that the bottleneck of your method is photo loading. 我想您的方法的瓶颈是照片加载。 Try to load everything except photos, and then show your activity, concurrently loading photos. 尝试加载除照片之外的所有内容,然后显示您的活动,同时加载照片。

Also you can try to create your own app table that contains just the data you need. 您也可以尝试创建自己的应用程序表,其中仅包含所需的数据。 So you'll do less selects while loading contacts. 因此,在加载联系人时您将减少选择。 But you would have to synchronize your table. 但是您将必须同步表。 You can do so concurrently. 您可以同时进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM