我正在使用Xamarin跨平台为iOS和Android构建应用程序.我有一个名为"Core"的项目,然后一个用于iOS,一个用于Android.
在共享的"核心"项目中,我想显示一个加载动画,同时从设备中获取当前位置.我可以获取我的locationWatcher,启动它,并直接调用LocationWatcher.CurrentPosition,但由于它刚刚开始观察位置更新,它还没有时间获取任何位置.
我的解决方案是在启动后等待2秒然后获取位置.但是我不想等待异步,我想在等待时锁定UI.这是因为用户在当前位置已知之前不应该做任何事情.
我目前通过循环来实现这一目标:
DateTime startTime = DateTime.Now();
while ( (DateTime.Now() - startTime).TotalMilliseconds() < 2000) {
//Locking the UI here, but it's what I want so it's ok.
}
Run Code Online (Sandbox Code Playgroud)
对于Java中的"Thread.sleep(2000)"来说,这似乎是一个丑陋的"黑客"
有没有更好的方法呢?
假设我有一个类似这样的方法的java类(只是一个例子)
@Transactional
public synchronized void onRequest(Request request) {
if (request.shouldAddBook()) {
if (database.getByName(request.getBook().getName()) == null) {
database.add(request.getBook());
} else {
throw new Exception("Cannot add book - book already exist");
}
} else if (request.shouldRemoveBook()) {
if (database.getByName(request.getBook().getName()) != null) {
removeBook();
} else {
throw new Exception("Cannot remove book - book doesn't exist");
}
}
}
Run Code Online (Sandbox Code Playgroud)
假设这本书被删除,然后重新添加了一个新作者或其他小改动,所以这个方法可能会从另一个系统快速调用两次,首先删除Book,然后再添加相同的Book(带有一些新的细节) ).
为了解决这个问题,我们可能会尝试(像我一样)添加上面的@Transactional代码,然后在@Transactional不起作用时也"同步".但奇怪的是,它在第二次通话中失败了
"无法添加书本已经存在".
我花了很多时间试图解决这个问题,所以我想我会分享答案.
我有两台服务器需要使用HTTPS互相交谈.
在这种情况下,我们将它们称为"服务器"和"客户端",其中"客户端正在对"服务器"进行https调用.
在生产中,服务器将具有有效的CA证书,但在测试时,我们将使用自签名证书.
据我所知,这是我们必须做的:
这一切都已完成,但在拨打电话时我收到此错误:
Caused by: javax.net.ssl.SSLPeerUnverifiedException: Host name 'docker-abc-123' does not match the certificate subject provided by the peer (CN=docker-abc-123, OU=unit, O=org, L=city, ST=area, C=xx)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:465) [httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:395) [httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) [httpclient-4.5.jar:4.5]
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:91) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:568) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
... 10 …Run Code Online (Sandbox Code Playgroud) android ×1
datetime ×1
docker ×1
httpclient ×1
https ×1
ios ×1
java ×1
self-signed ×1
sleep ×1
spring ×1
ssl ×1
synchronized ×1
xamarin ×1