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

Increase speed of parsing? #127

Closed
Ethan-Chew opened this issue Aug 8, 2020 · 2 comments
Closed

Increase speed of parsing? #127

Ethan-Chew opened this issue Aug 8, 2020 · 2 comments

Comments

@Ethan-Chew
Copy link

Hi, I was wondering if there is a way that I can parse the XLSX file faster as I am doing a 'Flashcard' type system in my app, and do not want a time delay when the user swipes.

` func getData() {
// Collect Data
var worksheetName = ""
worksheetName = "(primaryLevel) Data"

    do {
        let filepath = Bundle.main.path(forResource: "Main Data", ofType: "xlsx")!
        
        guard let file = XLSXFile(filepath: filepath) else {
            fatalError("XLSX file at \(filepath) is corrupted or does not exist")
        }
        
        for wbk in try file.parseWorkbooks() {
            guard let path = try file.parseWorksheetPathsAndNames(workbook: wbk)
                    .first(where: { $0.name == worksheetName }).map({ $0.path })
            else { continue }
            
            let sharedStrings = try file.parseSharedStrings()
            let worksheet = try file.parseWorksheet(at: path)
            
            if (topicSelRowStart + flashcardsIndex <= topicSelRowEnd) {
                currentFlashcard = worksheet.cells(atRows: [UInt(topicSelRowStart + flashcardsIndex)])
                    .compactMap { $0.stringValue(sharedStrings) }
                currentFlashcard = currentFlashcard.remove("Empty Cell")
            }
        }
    } catch {
        fatalError("\(error.localizedDescription)")
    }
    
    conceptName = currentFlashcard[2]
    
    conceptNameLabel.numberOfLines = 3; // Dynamic number of lines
    conceptNameLabel.lineBreakMode = NSLineBreakMode.byWordWrapping;
    conceptNameLabel.text = conceptName
            
    uneditedCurrentFlashcard = currentFlashcard
    currentFlashcard.removeSubrange(0..<4)
    
    let flashcardKnowledge = currentFlashcard.joined(separator: "\n")
    
    textField.text = "\(flashcardKnowledge)"
}

`

@MaxDesiatov
Copy link
Collaborator

Hi @Ethan-Chew, the main optimizations that could be applied are in XMLCoder, which is the foundational library for CoreXLSX. The latter basically provides only a model layer on top of the former to make parsing work.

In the meantime, if you parse the file frequently, I'd recommend caching the parsed values either in memory, or even on disk. If you consider the latter let me know, and I'll add Encodable conformance types that require it.

@Ethan-Chew
Copy link
Author

Hi @MaxDesiatov, I have found another solution that works, which is to store everything in a dictonary then grab it from there when I need to use it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants