Coder Social home page Coder Social logo

Comments (9)

EvanCooper9 avatar EvanCooper9 commented on June 26, 2024

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

from swift-week-view.

ludovicoloreti avatar ludovicoloreti commented on June 26, 2024

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

from swift-week-view.

EvanCooper9 avatar EvanCooper9 commented on June 26, 2024

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.

from swift-week-view.

ludovicoloreti avatar ludovicoloreti commented on June 26, 2024

God save @EvanCooper9
Thousands of thanks!

from swift-week-view.

EvanCooper9 avatar EvanCooper9 commented on June 26, 2024

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.

from swift-week-view.

ludovicoloreti avatar ludovicoloreti commented on June 26, 2024

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)

from swift-week-view.

EvanCooper9 avatar EvanCooper9 commented on June 26, 2024

@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 [].

from swift-week-view.

ludovicoloreti avatar ludovicoloreti commented on June 26, 2024

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

from swift-week-view.

EvanCooper9 avatar EvanCooper9 commented on June 26, 2024

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

from swift-week-view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.