How do I check the functions inside the table?

I am suffering from the error below.

When using display.loadRemoteImage(), there may be times when the table is already missing after loading the image.

(For example, after moving the screen or closing the window)

I check the function as follows, but it does not work well.

I can not avoid this problem because I think that display.loadRemoteImage() is not prepared for cancellation.

Please tell me how to check whether the function in the table is valid or how to cancel display.loadRemoteImage().


/Users/jenkins/slaveroot/workspace/Templates/label/android/subrepos/widget/widgetLibrary/widget_scrollview.lua:464: attempt to call method ‘insert’ (a nil value)


local scrollView = widget.newScrollView

{

  —

}

local function networkListener( event )

    if ( event.isError ) then

    else

        if event.status == 200 then

            if type(scrollView.insert) == “function” then

                scrollView:insert( event.target )

            end

        end

    end

end

display.loadRemoteImage( IMG_URL, “GET”, networkListener, IMG_NAME, system.TemporaryDirectory, 0, 0 )

  1. Welcome to the community.

  2. Please format all code posts.

formatyourcode.jpg

  1. You are actually on track, but your code is wrong.  

A. The best (IMHO) way to check if a display object is valid is to check that the removeSelf function still exists as it is common to all display objects.  

B. scrollView is not a display object.  It is a table with display objects as fields.  i.e. You need to do this instead:

if( scrollView.getView and type(scrollView:getView().removeSelf) == "function" ) then scrollView:insert( event.target ) end

Thank very much!

I will try this in this way.

Is it this for group and image?

(ex: group insert

if ( group1 and type(group1.removeSelf) == "function" ) then group1:insert( event.target ) end

(ex: sprite play

if ( img1 and type(img1.removeSelf) == "function" ) then img1:play() end

I tried it again, but it still got an error.

Is there a good way to do it?

/Users/jenkins/slaveroot/workspace/Templates/label/android/subrepos/widget/widgetLibrary/widget\_scrollview.lua:464: attempt to call method 'insert' (a nil value)

if( scrollView.getView and type(scrollView:getView().removeSelf) == "function" ) then   scrollView:insert( event.target ) end

All children of displayObject

I don’t know what to tell you.  I suspect you need to get help from someone by having them look at your entire project.  i.e. So they can run it and see what is going on.

What happens if you add a check for “ended” in the network listener and place the scrollView:insert(…) in there?

local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) elseif ( event.phase == "ended" ) then print( "Download complete, total bytes transferred: " .. event.bytesTransferred ) scrollView:insert( event.target ) end end

Thank you for telling me.

It works fine on my Mac / Windows and Android / iOS devices, but a small number of users of the app is causing this problem.

I will try event.phase == “ended” from now on.

Again the same error occurred.

/Users/jenkins/slaveroot/workspace/Templates/label/android/subrepos/widget/widgetLibrary/widget\_scrollview.lua:464: attempt to call method 'insert' (a nil value)

What I am doing is getting thumbnails from the list of items from the network, but the user may move out of this scene while acquiring.

At that time, Scrollview of the item list will be deleted, but an error will occur as the remote file acquired late will be inserted.

For this, I would like to check if the insert can be used in Scrollview.

You may want to look at network.download(…) and use a newImage(…) etc for it.  This way the network.download(…) call will give you a handle that you can use to ‘cancel’ the download.

Short overview:

At the top of the scene define the variable…

local handeId

…somewhere to start the download…

handleId = network.download(…blah blah…)

…in the scene:destroy( event ) …

network.cancel( handleId )

https://docs.coronalabs.com/api/library/network/download.html

I am using display.loadRemoteImage (), but can not I cancel this function?

If so, I would like to use network.download() and display.newImage() like your teachings.

Thank you very much for your advice!

  1. Welcome to the community.

  2. Please format all code posts.

formatyourcode.jpg

  1. You are actually on track, but your code is wrong.  

A. The best (IMHO) way to check if a display object is valid is to check that the removeSelf function still exists as it is common to all display objects.  

B. scrollView is not a display object.  It is a table with display objects as fields.  i.e. You need to do this instead:

if( scrollView.getView and type(scrollView:getView().removeSelf) == "function" ) then scrollView:insert( event.target ) end

Thank very much!

I will try this in this way.

Is it this for group and image?

(ex: group insert

if ( group1 and type(group1.removeSelf) == "function" ) then group1:insert( event.target ) end

(ex: sprite play

if ( img1 and type(img1.removeSelf) == "function" ) then img1:play() end

I tried it again, but it still got an error.

Is there a good way to do it?

/Users/jenkins/slaveroot/workspace/Templates/label/android/subrepos/widget/widgetLibrary/widget\_scrollview.lua:464: attempt to call method 'insert' (a nil value)

if( scrollView.getView and type(scrollView:getView().removeSelf) == "function" ) then   scrollView:insert( event.target ) end

All children of displayObject

I don’t know what to tell you.  I suspect you need to get help from someone by having them look at your entire project.  i.e. So they can run it and see what is going on.

What happens if you add a check for “ended” in the network listener and place the scrollView:insert(…) in there?

local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) elseif ( event.phase == "ended" ) then print( "Download complete, total bytes transferred: " .. event.bytesTransferred ) scrollView:insert( event.target ) end end

Thank you for telling me.

It works fine on my Mac / Windows and Android / iOS devices, but a small number of users of the app is causing this problem.

I will try event.phase == “ended” from now on.

Again the same error occurred.

/Users/jenkins/slaveroot/workspace/Templates/label/android/subrepos/widget/widgetLibrary/widget\_scrollview.lua:464: attempt to call method 'insert' (a nil value)

What I am doing is getting thumbnails from the list of items from the network, but the user may move out of this scene while acquiring.

At that time, Scrollview of the item list will be deleted, but an error will occur as the remote file acquired late will be inserted.

For this, I would like to check if the insert can be used in Scrollview.

You may want to look at network.download(…) and use a newImage(…) etc for it.  This way the network.download(…) call will give you a handle that you can use to ‘cancel’ the download.

Short overview:

At the top of the scene define the variable…

local handeId

…somewhere to start the download…

handleId = network.download(…blah blah…)

…in the scene:destroy( event ) …

network.cancel( handleId )

https://docs.coronalabs.com/api/library/network/download.html