问题 如果在ContentProvider的OnCreate中返回false,会发生什么?


该文档声明如果提供程序成功加载,我们应该返回true,否则返回false。在我的实现中,如果DatabaseHelper == null,我将返回false。

现在假设 DatabaseHelper == null 和  在onCreate中返回,稍后在代码中的某个位置查询提供程序,仍在查询提供程序,因为它会崩溃。

我的问题是在ContentProvider的OnCreate中返回false的用途是什么? 在onCreate失败后,我应该如何处理查询?在查询中再次运行onCreate?


4602
2017-12-11 04:22


起源



答案:


在ContentProvider的OnCreate中返回false有什么用?

通过快速浏览Android源代码,我发现, 就目前而言,你回来的并不重要, 它只是被忽略了再一次 就目前而言

关于测试和 ActivityThreadattachInfo 被称为之后 newInstance 所以,如果你看看 ContentProvider 资源 在第1058行 在哪 onCreate 被称为,看起来像:

/**
 * After being instantiated, this is called to tell the content provider
 * about itself.
 *
 * @param context The context this provider is running in
 * @param info Registered information about this content provider
 */
public void attachInfo(Context context, ProviderInfo info) {
    /*
     * We may be using AsyncTask from binder threads.  Make it init here
     * so its static handler is on the main thread.
     */
    AsyncTask.init();

    /*
     * Only allow it to be set once, so after the content service gives
     * this to us clients can't change it.
     */
    if (mContext == null) {
        mContext = context;
        mMyUid = Process.myUid();
        if (info != null) {
            setReadPermission(info.readPermission);
            setWritePermission(info.writePermission);
            setPathPermissions(info.pathPermissions);
            mExported = info.exported;
        }
        ContentProvider.this.onCreate();
    }
}

请记住,如果文档说明了谁知道,也许这将在将来的版本中使用/修复。


在onCreate失败后我应该如何处理查询?在查询中再次运行onCreate?

我会说是,不一定 onCreate 但你自己的方法初始化一次并确保你的 DatabaseHelper 或者说,那将是你最好的努力,我的意思是根据文件记录 onCreate

您应该推迟非常重要的初始化(例如打开,升级和扫描数据库),直到使用内容提供程序

所以从技术上来说,你会按照预期的方式进行,但它很疯狂,所以要安全。


12
2018-06-12 00:50



抱歉迟到了,很好的回答! - Bear


答案:


在ContentProvider的OnCreate中返回false有什么用?

通过快速浏览Android源代码,我发现, 就目前而言,你回来的并不重要, 它只是被忽略了再一次 就目前而言

关于测试和 ActivityThreadattachInfo 被称为之后 newInstance 所以,如果你看看 ContentProvider 资源 在第1058行 在哪 onCreate 被称为,看起来像:

/**
 * After being instantiated, this is called to tell the content provider
 * about itself.
 *
 * @param context The context this provider is running in
 * @param info Registered information about this content provider
 */
public void attachInfo(Context context, ProviderInfo info) {
    /*
     * We may be using AsyncTask from binder threads.  Make it init here
     * so its static handler is on the main thread.
     */
    AsyncTask.init();

    /*
     * Only allow it to be set once, so after the content service gives
     * this to us clients can't change it.
     */
    if (mContext == null) {
        mContext = context;
        mMyUid = Process.myUid();
        if (info != null) {
            setReadPermission(info.readPermission);
            setWritePermission(info.writePermission);
            setPathPermissions(info.pathPermissions);
            mExported = info.exported;
        }
        ContentProvider.this.onCreate();
    }
}

请记住,如果文档说明了谁知道,也许这将在将来的版本中使用/修复。


在onCreate失败后我应该如何处理查询?在查询中再次运行onCreate?

我会说是,不一定 onCreate 但你自己的方法初始化一次并确保你的 DatabaseHelper 或者说,那将是你最好的努力,我的意思是根据文件记录 onCreate

您应该推迟非常重要的初始化(例如打开,升级和扫描数据库),直到使用内容提供程序

所以从技术上来说,你会按照预期的方式进行,但它很疯狂,所以要安全。


12
2018-06-12 00:50



抱歉迟到了,很好的回答! - Bear