Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Firebase with your code #4

Closed
ludovicoloreti opened this issue Mar 10, 2018 · 9 comments
Closed

Using Firebase with your code #4

ludovicoloreti opened this issue Mar 10, 2018 · 9 comments
Assignees
Labels
inactive inactive label to track issues that will be soon closed

Comments

@ludovicoloreti
Copy link

Hello,
It's about 10 days I'm trying to figure out how to edit a little part of your code only to be able to use it with Firebase.
Firebase it's downloading all events already added in your way (WeekViewEvent) but the problem is the function weekViewGenerateEvents().
I really dunno how to solve it, I know the problem is the async and the sync. But really dunno how to solve.
Could you please help me just a little bit?

How can I use it synchronously? Thank you very much.
Best, best regards!

@EvanCooper9
Copy link
Owner

@ludovicoloreti could you post the code that you're using within weekViewGenerateEvents()?

@ludovicoloreti
Copy link
Author

ludovicoloreti commented Mar 10, 2018

yes of course! Thank you for your fast reply!


func weekViewGenerateEvents(_ weekView: WeekView, date: DateInRegion) -> [WeekViewEvent] {
        let db = dbRef.child("items").child(codiceUtente)
        db.observe(.value, with: { (snapshot) in
            guard let itemData = snapshot.children.allObjects as? [DataSnapshot] else {return}
            if (snapshot.exists() == false) {
                Utils.showAlert(title: "Nessun Dato", msg: "Nessun dato presente nel Database di Firebase!", in: self)
                return
            } else {
                for child in itemData {
                    guard let eventName = child.childSnapshot(forPath: "eventName").value as? String else {
                        Utils.showAlert(title: "Erroe campo eventName", msg: "Errore nel campo NOME_EVENTO (titolo evento), contattare amministratore!", in: self)
                        return
                    }
                    guard let startDate = child.childSnapshot(forPath: "startDate").value as? String else {
                        Utils.showAlert(title: "Erroe campo startDate", msg: "Errore nel campo DATA_INIZIO, contattare amministratore!", in: self)
                        return
                    }
                    guard let endDate = child.childSnapshot(forPath: "endDate").value as? String else {
                        Utils.showAlert(title: "Erroe campo endDate", msg: "Errore nel campo DATA_FINE, contattare amministratore!", in: self)
                        return
                    }
                    let initDate = DateInRegion(string: startDate, format: .custom("dd-MM-yyyy HH:mm"), fromRegion: self.regionRome)!
                    let finalDate = DateInRegion(string: endDate, format: .custom("dd-MM-yyyy HH:mm"), fromRegion: self.regionRome)!
                    let item: WeekViewEvent = WeekViewEvent(title: eventName, start: initDate, end: finalDate)
                    if date.day == initDate.day && date.day == finalDate.day {
                        self.items.append(item)
                    }
                    DispatchQueue.main.async {
                        print("CIAO",self.items)
//                        return self.items
                    }
                }
            }
        })
//        print("itemsssss",date.day ,"\n\n\n\n\n\n\n",items,"\n\n\n\n\n\n\n\n end itemszzz")
        return items
    }

The problem is (maybe the code is also not correct in the end where I want to print the array of items declared in the top of the viewcontrolller class like this: var items = [WeekViewEvent]()) my code is fired in different time into the weekViewGenerateEvents() function...

However the prob

@EvanCooper9
Copy link
Owner

Ok so the problem:

  • weeViewGenerateEvents runs synchronously
  • Firebase code runs asynchronously

This means that weeViewGenerateEvents has already returned before the firebase code has completed fetching your results. I'm working on a fix right now.

@ludovicoloreti
Copy link
Author

God save @EvanCooper9
Thousands of thanks!

@EvanCooper9
Copy link
Owner

I noticed also that for each event you're creating, you're returning that event. You should create all WeekViewEvents, store them in an array, and return that array.

I added eventCompletion: @escaping ([WeekViewEvent]) -> Void to the weekViewGenerateEvents method. It's not perfect but it will work for you. Once you've put all events for a single day in an array, you can do eventCompletion(events) to send them to the WeekView.

@ludovicoloreti
Copy link
Author

ludovicoloreti commented Mar 11, 2018

So you are saying that I can get my data from firebase inside your weekViewGenerateEvents(), and instead of the returning statement (you talked before that I've used it, (now obviously wrong)) I can literally append items to an array (like this array outside every functions: var items = [WeekViewEvent]()), then always inside your function I can compare all the array with your currently date (the input in your function, type DateInRegion) and add to something like a new event array with all the events in that date, and it will redo all of this I just said for every day in the printed WeekView.

Is it correct?

(and I add that maybe I could put my firebase data get outside that function because the array with events is always the same, but obviously I don't know how because it's async so I'm not able to do something like this, I'm able to do only the first part I told you before this brackets)

@EvanCooper9
Copy link
Owner

@ludovicoloreti You really have two options, so you can do one of the following.

Given that items is of type [WeekViewEvent]:

  1. Fetch all your data from Firebase outside of weekViewGenerateEvents(), then return the ones that match with date within the function like so: return items.
  2. Fetch all events that are related to date within weekViewGenerateEvents(), and use the asynchronous callback to pass these events to the WeekView like so : eventCompletion(items), and then return [].

@EvanCooper9 EvanCooper9 self-assigned this Mar 12, 2018
@ludovicoloreti
Copy link
Author

Last question:
Is it possible to add events directly without using weekViewGenerateEvents ?
In that way it will be simple because I will add events async and only one time, every event will be added. Sorry and thanks

@EvanCooper9
Copy link
Owner

Hey, is this still an issue? If so, have a look at the newest update

@EvanCooper9 EvanCooper9 added the inactive inactive label to track issues that will be soon closed label Jan 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inactive inactive label to track issues that will be soon closed
Projects
None yet
Development

No branches or pull requests

2 participants