Skip to content

Commit

Permalink
处理多线程问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoker1 committed Jun 29, 2022
1 parent 2fc33d6 commit df5c1ef
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableEmitter;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

/**
* Encapsulates communication with SDK KeyManager for SDKKeys.
Expand All @@ -59,6 +60,13 @@ public class DJISDKModel {
private Map<Object, List<KeyListener>> keyListeners;
//endregion

static {
//设置所有RX的全局Error代理,为了避免某些Widget不设置errorConsumer而导致crash。或者类似Completable.mergeArray只能处理其中一个error,
// 其他error都是需要全局错误处理器处理。
// 如果某个Widget有添加特定的error,则调用特定的error;否则调用全局的error
RxJavaPlugins.setErrorHandler(throwable -> DJILog.e(TAG, throwable.getMessage()));
}

private DJISDKModel() {
keyListeners = new ConcurrentHashMap<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected void addModule(@NonNull BaseModule baseModule) {
/**
* Set up the widget model by initializing all the required resources
*/
public void setup() {
public synchronized void setup() {
if (isStarted()) {
throw new IllegalStateException("WidgetModel is already setup. Call cleanup first.");
}
Expand All @@ -110,7 +110,7 @@ public void setup() {
/**
* Clean up the widget model by destroying all the resources used
*/
public void cleanup() {
public synchronized void cleanup() {
if (keyDisposables != null) {
keyDisposables.dispose();
keyDisposables = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ class CompassWidgetModel(djiSdkModel: DJISDKModel,
// Do nothing
}

override fun onLocationChanged(p0: Location) {
override fun onLocationChanged(location: Location) {
// Update the center type to be the RC/Mobile device type
centerTypeProcessor.onNext(CenterType.RC_MOBILE_GPS)
// Update location using received location of the mobile device
rcOrMobileLatitude = p0.latitude
rcOrMobileLongitude = p0.longitude
rcOrMobileLatitude = location.latitude
rcOrMobileLongitude = location.longitude
updateCalculations()
updateStates()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ class GPSSignalWidgetModel(
}

override fun updateStates() {
rtkEnabledProcessor.value?.let {
rtkEnabledProcessor.value.let {
if (it) {
satelliteNumberProcessor.onNext(rtkStateProcessor.value?.mobileStation1SatelliteCount ?: 0)
satelliteNumberProcessor.onNext(rtkStateProcessor.value.mobileStation1SatelliteCount)
} else {
satelliteNumberProcessor.onNext(satelliteCountProcessor.value ?: 0)
satelliteNumberProcessor.onNext(satelliteCountProcessor.value)
}
}
}
Expand Down

0 comments on commit df5c1ef

Please sign in to comment.