[当我调用该事件时,将其包装在一个Observable中,然后将其传递给映射它的服务,然后通过活动调用此服务。
我遇到的一个问题是,当启动新的Observable并调用Event时,该Event被返回,但随后以null的形式传递给Observable.next,这导致我的应用程序崩溃。调试时,我可以看到日历事件,因此我觉得这是一些奇怪的异步/线程问题。
这里是活动的召唤:
@Module
class GoogleCalendarsApiServiceModule @Inject constructor(): IGoogleCalendarsApiServiceModule {
private lateinit var googleCalendarService: Calendar
private val httpTransport: NetHttpTransport = NetHttpTransport()
private val subscriptions: Disposable = Disposable.empty()
init {
this.subscriptions.apply {
AndroidContextObservable.context()
.subscribe { context ->
val googleCredential = GoogleCredentialsBuilder.getCredentials(httpTransport, context)
googleCalendarService = ServiceBuilder.buildGoogleCalendarService(googleCredential, httpTransport)
}
}
}
fun finalize() {
this.subscriptions.dispose()
}
override fun getEventById(eventId: String): Observable<Event> {
return Observable.create { observer ->
val calendarEvents: Calendar.Events? = googleCalendarService.events()
val event = calendarEvents!!
.get("primary", eventId)
.execute()
observer.onNext(event)
observer.onComplete()
}
}
}
然后是映射事件的呼叫服务:
override fun getCalendarEventById(eventId: String): Observable<FullMeetingEvent> {
return this.googleCalendarsApiServiceModule.getEventById(eventId)
.map { mapMeetingEvent(it) } // <-- skipped due to event being null/empty runnable
.onErrorReturn { throw Exception(it) }
}
然后是最后的调用活动:
class ViewMeetingEventDataActivity: AppCompatActivity() {
@Inject lateinit var bundleUtilityModule: BundleUtilityModule
@Inject lateinit var fragmentUtilityModule: FragmentUtilityModule
@Inject lateinit var googleCalendarService: GoogleCalendarService
private val subscriptions: Disposable = Disposable.empty()
private lateinit var eventId: String
private lateinit var event: FullMeetingEvent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidContextObservable.setContext(this)
DaggerServiceModuleComponent.create().inject(this)
DaggerUtilityModuleComponent.create().inject(this)
this.eventId = intent.getStringExtra(BundleData.MEETING_EVENT_ID.name)!!
setContentView(R.layout.layout_view_meeting_event_activity)
}
override fun onStart() {
super.onStart()
subscriptions.apply {
googleCalendarService.getCalendarEventById(eventId)
.subscribeOn(Schedulers.io())
.subscribe(
{
event = it
setViewMeetingEventFragment(it)
},
{err -> println(err)}
)
}
}
override fun onDestroy() {
super.onDestroy()
this.subscriptions.dispose()
}
private fun setViewMeetingEventFragment(meetingEvent: FullMeetingEvent) {
fragmentUtilityModule.createSingleFragment<FullMeetingEvent, ViewMeetingEventDataFragment>(meetingEvent, BundleData.FULL_MEETING_EVENT.name, supportFragmentManager, R.id.view_meeting_event_data_container)
}
}
显然,当服务失败时,活动中的订阅将直接进入错误块。
我知道我有这个事件,因为在调试器中四处搜寻,所以设法找到了它:
但是正如您在调试器上方的代码中看到的那样,它显然创建了一个可观察的null。
这就像可观察对象在事件调用结束之前就已触发,但是我的代码可以完全相同的方式捕获所有事件,并且工作正常。
我正在通过ID从Google日历V3 Java API通过ID获取日历事件,该API是以下程序的包装:https://developers.google.com/calendar/v3/reference/events/get?apix_params=%7B%22calendarId %22%3A%...