GreenDao之Identity scope and session “cache”

原文http://greendao-orm.com/documentation/sessions/

        问题:

DaoMaster dm = AppContext.getDaoMaster(ctx);
DaoSession ds = dm.newSession(IdentityScopeType.Session);

        如果传入的是IdentityScopeType.Session,同样的查询只会执行一次,查询结果对象List保存在内存里会重复使用。会导致的问题是如果List对象属性改变,未持久化它,再次做query,不会从数据库查询,只是缓存中的结果。会导致与数据库表数据不一致。

       解决办法,再次执行查询之前执行ds.clear();方法。或者在这种情况下之不保留缓存,用IdentityScopeType.None。

<span style="white-space:pre">	</span>public List<Label> queryLabels(){
		DaoSession ds = AppContext.getDaoSession(ctx);
//		//clear the cache,requery the data from database table, not from the memory.
		ds.clear();
		
		QueryBuilder<Label> qb = ds.getLabelDao().queryBuilder();
		qb.where(LabelDao.Properties.Deleted.eq(false));
		List<Label> labels = qb.list();
		if(labels.size()<showNum){
			Label entity = new Label(null, "添加标签", null, false, null, null, true, false);
			labels.add(entity);
		}

		
		return labels;
	}

或者用IdentityScopeType.None创建DaoSession


<span style="white-space:pre">	</span>private DaoSession getDaoSession(IdentityScopeType scope){
		if(scope == IdentityScopeType.Session){
			DaoSession ds = AppContext.getDaoSession(ctx);
			return ds;
		}else{
			DaoMaster dm = AppContext.getDaoMaster(ctx);
			DaoSession ds = dm.newSession(IdentityScopeType.None);
			return ds;
		}
	}


<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="DB-NetShopsConnectionString" connectionString="Data Source=.\MSSQLSERVER2012;Initial Catalog=DB-NetShops;User ID=sa;Password=your_password;Integrated Security=False" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <!-- 添加全局cookie设置 --> <httpCookies httpOnlyCookies="true" /> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" maxRequestLength="20480" executionTimeout="300" requestValidationMode="4.7.2" /> <!-- 移除httpOnlyCookies属性 --> <sessionState mode="InProc" timeout="20" /> <authentication mode="Forms"> <!-- 移除httpOnlyCookies属性 --> <forms loginUrl="~/WebForm1.aspx" defaultUrl="~/WebForm2.aspx" timeout="20" /> </authentication> <globalization culture="zh-CN" uiCulture="zh-CN" requestEncoding="utf-8" responseEncoding="utf-8" /> <customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx"> <error statusCode="404" redirect="~/404.aspx" /> <error statusCode="500" redirect="~/500.aspx" /> </customErrors> <!-- 新添加的母版页配置 --> <pages masterPageFile="~/Site.master"> <controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </controls> </pages> </system.web> <system.webServer> <httpProtocol> <customHeaders> <add name="X-Content-Type-Options" value="nosniff" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> <add name="X-Xss-Protection" value="1; mode=block" /> </customHeaders> </httpProtocol> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent> <security> <requestFiltering> <requestLimits maxAllowedContentLength="20971520" /> </requestFiltering> </security> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> </compilers> </system.codedom> </configuration> 根据webconfig给出完整的数据库代码
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值