2012-12-11 48 views
11

該文檔指出,如果提供程序成功加載,我們應該返回true,否則返回false。在我的實現中,如果DatabaseHelper == null,我會返回false。如果在ContentProvider的OnCreate中返回false,會發生什麼情況?

假設現在DatabaseHelper == null中的onCreate返回,並在後面的代碼某處查詢提供者,提供者仍然被質疑和堂妹就會崩潰。

我的問題是在ContentProvider的OnCreate中返回false的用法是什麼? 如何在失敗onCreate後處理查詢?在查詢中再次運行onCreate?

回答

12

在ContentProvider的OnCreate中返回false是什麼用途?

通過通過Android源快速導航我發現,至於現在,真的不要緊,你回什麼,它只是被忽略,再次至於現在

在測試和ActivityThreadattachInfonewInstance後立即調用,所以如果你看一下ContentProviderat line 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

你的文件說的是,不一定onCreate,但你自己的方法來初始化一次,並確保您的DatabaseHelper左右,這將是你最大的努力應該推遲平凡的初始化(例如打開,升級和掃描數據庫)直到使用內容提供者

因此,從技術角度而言,你會按預期做,但它是野外的這裏很安全。

+0

對不起,遲到接受,很好的答案! – Bear

相關問題