我正在使用react-navigation导航屏幕。我有以下带有onPress
属性的按钮标记,如果按下它,将调用navigate
,它应该转到Downloading
屏幕。之后,调用generateReports()
函数,传递回调函数。
<Button transparent onPress={() => {
navigate('Downloading')
generateReports(() => navigate('Home'))
}}>
export const generateReports = (callback) => {
generateReportsTable(callback)
}
export const generateReportsTable = (callback) => {
let currentYear = new Date(new Date().getFullYear(), 0, 1);
let organizations = realm.objects('Organization')
let tickets = realm.objects('Ticket')
let currentYearTickets = tickets.filtered('created_at > $0', new
Date(currentYear).getTime())
for(organization of organizations) {
let currentOrganizationTickets = []
console.log(organization.name)
for(ticket of currentYearTickets) {
if(ticket.organization_id === organization.id) {
currentOrganizationTickets.push(ticket)
}
}
console.log(currentOrganizationTickets)
}
callback()
}
然而,当我运行代码并按下按钮时,应用程序会挂起,直到generateReports
的过程完成并进行导航。我错过了什么?我需要先导航,然后执行generateReports()