A function returning file contents?

AI2 as it stands is broken. I don't believe all the detours suggested make even the slightest bit of sense. This gigantic hole needs to be fixed in its base. I think it is best to just try to forget that I ever looked into this mess.

I don't understand why we don't get a simple example (at least of the text file and the expected result) to see what it's all about. And please also post a small test aia.
I have to read here for a long time to guess what this topic could be about.

Note: Make it as easy as possible for those who are helping.
See also here.

1 Like

Please write a function to return the contents of a file as a list. It doesn't appear to be possible in AI2. See the picture I posted above (can't get much simpler than that). There is no way to get the result from the 'when' block back to the function for it to return it to the calling block without blocking the event loop. The code in the picture returns global filedata even if the 'when' block hasn't yet been activated to set it to the contents of the file. It doesn't allow the event loop to run and activate the 'when' block if the disabled block is enabled.

The suggestions I made in the second paragraph of the original post allow AI2 to be a useful "language". When I say 'wait' (or 'sleep') I don't mean an idle loop, but instead am intending for it to add an event to the queue with execution proceeding immediately after the 'wait' block (like the current AI2 event proceed to a point immediately following a 'when' block) upon the event firing. A tcl 'update' is somewhat similar as it defers execution back to the event loop and gets scheduled to resume when the event queue has been otherwise been emptied.

For those who think in terms of pictures of blocks, the language needs to add a 'when' block that has the shape of the 'set' blocks shown in the picture. I gave it a different name because it also indicates the blocks processing is complete until the event fires at some time in the future.

You could build your own extension to do this

1 Like

Why use AI2 if I am already working in a capable language?

I'm currently working on such an extension.

See this also:

Please show us your AIA or a test AIA and show the other blocks as well.

It is possible. I'll show the blocks a bit later.

Looking forward to seeing the blocks.

If that is the case, why are you asking this question in the first place !!

It was suggested that the solution to the problem was to write an extension in Java.

AI2 is written in blocky/javascript and java. Most if not all components are built using java.

Have a look at the sources....

I'm just saying if I have to write an extension in Java, then I might as well write the whole app in Java. I already have a complete flow chart.

As you found out yourself, reading a file works asynchronously... read the file and after a little bit of time depending on the size of the file you get the content of the file back in the event GotFile

If you think you have to duplicate blocks to get a result, you are doing something wrong... are you familiar with procedures? it looks like you first have to understand more about the architecture of App Inventor ... see again the links provided earlier in this thread by @ABG ...

And what about following the suggestion from @Anke

You said

This is not necessary, the only thing you have to do is to design your logic event orientated...

Taifun

4 Likes

are you familiar with procedures?

I was asking for a function. Don't you think I might know about procedures?

The result is required inside a loop which is inside another loop. The logic is made manageable with the requested function. The only thing causing the problem is the fact that the file operation has been artificially split into two parts.

It is impossible to write code for a function returning the contents of a file? That is the complete nature of my question.

why the blocks had to be in one part if two parts works fine?
in app inventor async operation needs a separate event block to get the result, at least before anonymous procedure is implemented.

I found a survey article covering attempts to do stunts like this in various languages.

It's a fun read.
Don't miss the Pyramid of Doom.

1 Like

concurrent in a single-thread?

The main processing block can't continue until the file contents are available. Memory limits of the device preclude reading all the files in advance. Since it needs the file contents it calls the 'readDataFile' function. Because the file read operation is split into two blocks, 'File.Readfrom' and 'File.GotText', there is no way to get the result back to the main routine that called 'readDataFile' (without introducing a polling loop).

A 'when' (I would call this block a 'wait-for-event') block shaped like a 'set' block that could be placed into the function definition could fix the problem. There are other solutions also, but I don't see a way to make this happen in AI2 as it stands now.

I didn't see the second line before. Can you tell me more about 'anonymous procedure'?

i mean this:

People keep asking to see my blocks. I have been ignoring the request because I think showing you 1200 blocks is not going to help you answer my question. I have implemented the first three items on my requirement list and then tested. Everything was working. Don't assume I don't understand what I am doing. When I started implementing the fourth requirement (where the file stuff begins), I discovered the odd way in which files are handled in AI2. I've included a screen shot of part of my Screen1. You don't need the details to answer my question. I have asked the question that deals with the essential issue.

I would guess that fewer than 50 blocks from this has anything to do with the files part.