0

我正在为我的NativeScript应用程序编写一个 angular 2 服务,用于检索所有用户的联系人。我当前的实现使用了一种同步getContentResolver方法,例如nativescript-contacts中使用的方法。报告cursor.getCount超过 7k 游标并冻结应用程序总共约 3 秒。不好。

我正在遵循本指南,使用-loaders-in-android,到目前为止还没有运气。

这是我到目前为止所拥有的:

declare let android: any
declare let java: any

import * as application from "application"
import {Injectable} from "@angular/core"

@Injectable()
export class ContactsService
    extends android.support.v4.app.LoaderManager
    implements android.support.v4.app.LoaderManager.LoaderCallbacks {

    private contactsLoader: any = new android.support.v4.app.LoaderManager.LoaderCallbacks(
        class extends android.support.v4.app.LoaderManager.LoaderCallbacks {
            onCreateLoader(id, args) {
                let projection: Array<string> = [
                    android.provider.ContactsContract.RawContactsColumns.CONTACT_ID,
                    android.provider.ContactsContract.ContactsColumns.DISPLAY_NAME,
                    android.provider.ContactsContract.ContactsColumns.HAS_PHONE_NUMBER,
                    android.provider.ContactsContract.ContactsColumns.LOOKUP_KEY,
                    android.provider.ContactsContract.ContactsColumns.PHOTO_URI,
                    android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER,
                    android.provider.ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
                    android.provider.ContactsContract.CommonDataKinds.Phone.TYPE,
                    android.provider.ContactsContract.DataColumns.MIMETYPE,
                ]
                return new android.support.v4.content.CursorLoader(
                    application.android.foregroundActivity,
                    android.provider.ContactsContract.Data.CONTENT_URI,
                    'data2 IS 2',
                    null,
                    null
                ).loadInBackground()

            }

            onLoadFinished(param0: android.support.v4.content.Loader, param1: java.lang.Object): void {
                global.tnsconsole.dump('onLoadFinished > param0', param0)
                global.tnsconsole.dump('onLoadFinished > param1', param1)
            }

            onLoaderReset(param0: android.support.v4.content.Loader): void {
                global.tnsconsole.dump('onLoaderReset > param0', param0)
            }
        }
    )

    getContactsAsync(): Promise<any> {
        // since the initLoader method is part of android.support.v4.app.LoaderManager
        this.initLoader(1, null, this)
        // but i get this error :(
        // EXCEPTION: Error: Cannot convert object to Landroid/support/v4/app/LoaderManager$LoaderCallbacks; at index 2
        return Promise.resolve([])
    }

}

有谁知道如何正确实现LoaderManager.LoaderCallbacks,以便我可以在后台线程中运行光标?

谢谢!

4

0 回答 0