Macaw icon indicating copy to clipboard operation
Macaw copied to clipboard

Sharing Node instance across different SVGViews causes infinite loop

Open lubiluk opened this issue 8 years ago • 12 comments

Hi, I use Macaw (great library) in such a way that I download SVG files from the internet to memory cache, turn them into Macaw Nodes, manipulate them to alter their colors and then use them as icons in a table view.

The issue

I noticed that when one Node instance appears in more than one SVGView the main thread enters an infinite loop (somewhere in drawRect).

Discussion

It looks like Macaw doesn't really like using Nodes directly. For instance, once a node is set on MacawView, there's no way to nil it out, so I had to use an awkward construction like this:

if let icon = model.icon {
    titleCell.iconSvgView.node = icon
} else {
    titleCell.iconSvgView.fileName = nil
}

I can of course try to redesign my code so it saves images on the disk but isn't it worth discussing why a flow that would be typical for UIImage does not work for Macaw.Node?

lubiluk avatar Oct 11 '18 06:10 lubiluk

Hi, @lubiluk, you can use MacawView instead of SVGView, but get Node from SVGparser:

open class func parse(path: String, ofType: String = "svg") throws -> Node

But avoid of parsing SVG each time, it can take some resources, better cache parsed nodes.

amarunko avatar Oct 11 '18 12:10 amarunko

@amarunko Thanks for the suggestion. Unfortunately, MacawView also enters an infinite loop when shares a node with another MacawView (or SVGView).

Tested like so:

class ViewController: UIViewController {
    @IBOutlet weak var view1: MacawView!
    @IBOutlet weak var view2: MacawView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://css-tricks.com/wp-content/uploads/2015/05/kiwi.svg")!
        let svg = try! String(contentsOf: url, encoding: .utf8)
        
        let node = try! SVGParser.parse(text: svg)
        
        view1.node = node
        view2.node = node
    }
}

Macaw version 0.9.3.

lubiluk avatar Oct 11 '18 12:10 lubiluk

Did you try copying of node for quick fix?

func copyNode(_ referenceNode: Node) -> Node? {
        let pos = referenceNode.place
        let opaque = referenceNode.opaque
        let visible = referenceNode.visible
        let clip = referenceNode.clip
        let tag = referenceNode.tag

        if let shape = referenceNode as? Shape {
            return Shape(form: shape.form, fill: shape.fill, stroke: shape.stroke, place: pos, opaque: opaque, clip: clip, visible: visible, tag: tag)
        }
        if let text = referenceNode as? Text {
            return Text(text: text.text, font: text.font, fill: text.fill, stroke: text.stroke, align: text.align, baseline: text.baseline, place: pos, opaque: opaque, clip: clip, visible: visible, tag: tag)
        }
        if let image = referenceNode as? Image {
            return Image(src: image.src, xAlign: image.xAlign, yAlign: image.yAlign, aspectRatio: image.aspectRatio, w: image.w, h: image.h, place: pos, opaque: opaque, clip: clip, visible: visible, tag: tag)
        }
        if let group = referenceNode as? Group {
            var contents = [Node]()
            group.contents.forEach { node in
                if let copy = copyNode(node) {
                    contents.append(copy)
                }
            }
            return Group(contents: contents, place: pos, opaque: opaque, clip: clip, visible: visible, tag: tag)
        }
        return .none
    }

About content scale - very strange, it can be a bug, did you set content mode after updating node?

amarunko avatar Oct 11 '18 12:10 amarunko

I will try to test case with one shared node for 2 or more MacawViews. I hope we can fix this loops.

amarunko avatar Oct 11 '18 12:10 amarunko

Thanks, I was searching for a way to copy a node.

Content mode works well if I set it in code, it just ignores the value set in interface builder. SVGView however respects setting from interface builder.

lubiluk avatar Oct 11 '18 12:10 lubiluk

Hey, I think the issue with infinite loop is somewhat more complex. I kept getting problems with that, so I decided to convert everything to UIImage before using on screen. Unfortunately now my app freezes in the code that converts SVG into UIImage.

What I've found so far:

  1. Multiple MacawViews / SVGViews on screen cause problems when share one Node
  2. Multiple MacawViews / SVGViews on screen cause problems when they don't share Nodes but nodes are reassigned to them
  3. SVGParser causes problems when used in parallel in two or more threads.

I'm attaching a screenshot from profiler. NodesMap seems to be guilty somewhat in case of SVGParser. For Macaw view the tree in profiler is so complex that I don't know what's going on yet.

screen shot 2018-10-12 at 16 05 35

I hope you find this useful.

lubiluk avatar Oct 12 '18 14:10 lubiluk

Unfortunately, SVGParser works well only on the main thread, we plan some multithreading work a little bit later. Thanks for deeper investigation. Did you use the release version of Macaw?

amarunko avatar Oct 12 '18 16:10 amarunko

I tried release version 0.9.3 as well as latest master. Both installed with carthage.

lubiluk avatar Oct 13 '18 20:10 lubiluk

Hello lubiluk, We introduced some major changes to inner Macaw organization which allowed the usage of one node in multiple places. You are welcome to try it out. Please let us know if there are still problems.

f3dm76 avatar Nov 29 '18 06:11 f3dm76

Should be working in the 0.9.4 release.

ystrot avatar Feb 14 '19 12:02 ystrot

I've faced with same issue in the 0.9.4 release.

K-Be avatar Feb 27 '19 03:02 K-Be

Still facing this

Sega-Zero avatar Feb 09 '20 10:02 Sega-Zero