getcurrentinstance
getinstance() ÒªÁìÓÃÓÚ»ñÈ¡Ä¿½ñÊý¾Ý¿âʵÀý£¬¿ÉÒÔÖ´ÐÐÒÔϲÙ×÷£º»á¼ûÊý¾Ý¿â±í¡£Ö´ÐÐÊý¾Ý¿âÅÌÎÊ¡£²åÈë¡¢¸üлòɾ³ýÊý¾Ý¡£
GetInstance() ÒªÁ죺»ñÈ¡Ä¿½ñÊý¾Ý¿âʵÀý
СÐò
ÔÚÒƶ¯Ó¦Óÿª·¢ÖУ¬Ê¹ÓÃÊý¾Ý¿âÀ´´æ´¢ºÍÖÎÀíÊý¾ÝÖÁ¹ØÖ÷Òª¡£Android ÌṩÁËÒ»¸öÃûΪ Room µÄ¿â£¬ÓÃÓÚ¼ò»¯ SQLite Êý¾Ý¿âµÄ½»»¥¡£GetInstance() ÒªÁìÊÇ Room Êý¾Ý¿âÀàÖÐÒ»¸öÖ÷ÒªµÄº¯Êý£¬ÓÃÓÚ»ñÈ¡Ä¿½ñÊý¾Ý¿âʵÀý¡£
GetInstance() ÒªÁìµÄÓÃ;
GetInstance() ÒªÁìÓÃÓÚÔÚÓ¦ÓóÌÐòÐèÒª»á¼ûÊý¾Ý¿âʱ¼ìË÷Ä¿½ñÊý¾Ý¿âʵÀý¡£Ëü·µ»ØÒ»¸ö RoomDatabase µÄʵÀý£¬¸ÃʵÀýÌṩ¶ÔÊý¾Ý¿â±íµÄ»á¼ûȨÏÞ¡£
ŲÓà GetInstance() ÒªÁì
ҪŲÓà GetInstance() ÒªÁ죬ÇëʹÓÃÒÔÏÂÓï·¨£º
MyDatabase.getInstance(context)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
- MyDatabase ÊÇ°üÀ¨Êý¾Ý¿â±íºÍ²Ù×÷½ç˵µÄ Room Êý¾Ý¿âÀà¡£
- context ÊÇÄ¿½ñÓ¦ÓóÌÐòÉÏÏÂÎĵÄÒýÓá£
ʾÀý
ÀýÈ磬ÒÔÏ´úÂëƬ¶ÏչʾÁËÔõÑùʹÓà GetInstance() ÒªÁì»ñÈ¡Êý¾Ý¿âʵÀý£º
class MyActivity extends Activity { private MyDatabase database; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); database = MyDatabase.getInstance(this); } }
µÇ¼ºó¸´ÖÆ
ʹÓà GetInstance() ʵÀý
»ñÈ¡Êý¾Ý¿âʵÀýºó£¬¿ÉÒÔʹÓÃËüÀ´Ö´ÐÐÒÔϲÙ×÷£º
- »á¼ûÊý¾Ý¿â±í£¨Í¨¹ý DAO ½Ó¿Ú£©¡£
- Ö´ÐÐÊý¾Ý¿âÅÌÎÊ¡£
- ²åÈë¡¢¸üлòɾ³ýÊý¾Ý¡£
×¢ÖØÊÂÏî
- GetInstance() ÒªÁìÊÇÏß³ÌÇå¾²µÄ£¬ÕâÒâζ×Å¿ÉÒÔ´Ó¶à¸öÏß³ÌͬʱŲÓÃËü¡£
- ÿ´ÎŲÓà GetInstance() ÒªÁìʱ£¬Ëü¶¼»á·µ»ØͳһÊý¾Ý¿âʵÀý¡£
- ÈôÊÇÊý¾Ý¿âÉÐ佨É裬GetInstance() ÒªÁ콫×Ô¶¯½¨ÉèËü¡£
ÒÔÉϾÍÊÇgetcurrentinstanceµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡