Synchronous vs Asynchronous processing in Flash

dyarosh
dyarosh used Ask the Experts™
on
I'm new to Flash and have a problem that I'm not sure how to solve.  I have a search function in my flash program that searches an XML file for the criteria entered.  So far so good.  That part works.  The XML file contains info on many abstracts such as the title, authors and a synopsis of the abstract.  The criteria entered is searched in each abstract and if found, returns the abstract and proceeds to the next abstract.  If the criteria is not found, then I want to load the abstract (txt file) and search the entire abstract for the criteria.  I have the code for loading the abstract (shown below) but I don't know how to halt processing until the abstract is loaded and searched.  In other words, I don't want to move on to the next abstract until the current abstract has been completely searched.

Any help would be greatly appreciated.
// Import Global variables
import AAPglobal;

//disable not found text
notFound_Text.visible = false;

//handle the button click
SearchBtn.addEventListener(MouseEvent.CLICK, searchAbstracts);
SearchBtn.useHandCursor = true;
stop();

//------------------ Functions ----------------------------
function searchAbstracts(e:Event):void
{
	// For each abstract, check the Title, Synopsis, Authors and Affiliations for the keywords entered.
	// Once a keyword is found in an abstract, the next abstract is checked.  Don't need to check each keyword
	// once one is found in an abstract.
	var abstractCount:Number = AAPglobal.AbstractList.length();		// Get the number of Abstracts
	var wordFound:Boolean = false;									// True if word found in Abstract
	var searchFound:Boolean = false;								// True if any abstracts found
	var delimFound:Number = -1;										// Greater than -1 if ; separator found in word list
	var wordSearch:String = "";										// Holds the words to find
	var wordStr:String = "";										// Holds the individual search string
	var titleStr:String = "";										// Holds the Title String Converted to uppercase
	var synopsisStr:String = "";									// Holds the Synopsis String Converted to uppercase
	var authorsStr:String = "";										// Holds the Authors String converted to uppercase
	var affiliationsStr:String = "";								// Holds the Affiliations String converted to uppercase
	var temparray:Array = new Array();								// Holds the abstract numbers found during search
	var fullSearchFound = -1;
	
	// Loop through each Abstract
	for (var i:Number = 0; i < abstractCount; i++)
	{
		// Add ; to end of string in case user forgets to put one
		wordSearch = searchFor.text + ";";

		// Convert words to uppercase for comparison
		wordSearch = wordSearch.toUpperCase()
		
		// Loop through each word in the search
		while (wordSearch != "")
		{
			// Set wordFound to false for each search criteria
			wordFound = false;
			
			// Find the ; delimeter
			delimFound = wordSearch.indexOf(";");
	
			// If found, there is a word to search for
			if (delimFound > 0)
			{
				// Get the search string
				wordStr = wordSearch.substring(0, delimFound);
				trace(wordStr);
				
				// Search Title and Synopsis for the search string
				titleStr = AAPglobal.AbstractList[i].title.toUpperCase();
				synopsisStr = AAPglobal.AbstractList[i].synopsis.toUpperCase();
				if (titleStr.search(wordStr) > -1 || synopsisStr.search(wordStr) > -1)
				{
					wordFound = true;
					trace("wordFound in title/synopsis");
					searchFound = true;
					break;		// breaks from While loop
				}		// end if
				
				// Search Authors and Affiliations
				for (var j:Number = 0; j < AAPglobal.AbstractList[i].authors.length(); j++)
				{
					authorsStr = AAPglobal.AbstractList[i].author[j].name.toUpperCase();
					affiliationsStr = AAPglobal.AbstractList[i].author[j].affiliations.toUpperCase();
					if (authorsStr.search(wordStr) > -1 || affiliationsStr.search(wordStr) > -1)
					{
						wordFound = true;
						trace("wordFound in Authors");
						searchFound = true;
						break;			// breaks from for j loop
					}		// end if				
				}  		// end for j loop
			}		// end if delim 
			
			// If search string found, move to next abstract, don't need to search for any more words
			if (wordFound) {
				break;		// break while loop
			}
			
			// Remove searched word from list
			wordSearch = wordSearch.substr(delimFound+1); 
		}		// end while loop
		
		// If search criteria found in abstract, add to temp_array
		if (wordFound) {
			// Search criteria found in abstract, add abstract to list
			trace("wordFound in Abstract " + AAPglobal.AbstractList[i].abstractnumber.toString());
			temparray.push(AAPglobal.AbstractList[i].abstractnumber.toString())
			searchFound = true;
		} else {
			// Search criteria not found in quick search of abstract, load entire abstract text and search again
			trace("Searching Full Abstract");
			wordSearch = searchFor.text + ";";
			fullSearchFound = -1;
			fullSearchFound = FullSearch(AAPglobal.AbstractList[i].abstractnumber.toString(), AAPglobal.AbstractList[i].searchTXT, wordSearch);
			if (fullSearchFound > -1)
			{
				trace("Search Criteria found in Full Search in Abstract " + fullSearchFound);
			}
		}
	}		// end for i loop

	// If any search criteria found, display abstracts
	if (searchFound) {
		AAPglobal.AbstractSearchResults = temparray;
		AAPglobal.SearchinProgress = true;
		gotoAndPlay("titlesList");
	}
	else {
		notFound_Text.visible = true;
	}
}

function FullSearch(abstractNO:String, fileName:String, searchCriteria:String):Number
{
	//load the abstract text file
	var textLoader:URLLoader = new URLLoader();							// variable used to load text file
	var textReq:URLRequest = new URLRequest(fileName);					// set request to abstract file text name

	// Define the loader complete event
	textLoader.addEventListener(Event.COMPLETE, textLoadComplete);
	textLoader.load(textReq);

	// This is where I want to put the code to finish searching the abstract but if I use an Event Listener,
	// program flow will go to the textLoadComplete function.  

}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
first thing I would do is place your for() loop in another function.  If searching doesn't return positive results, save your 'i' variable in another variable, and set i to the abstractCount (to close the for() loop). Within the loop, call your fullSearch(), and once loaded search the full abstract.  when the full searching finishes, call the function that you placed the for loop in, and pass the variable that stores the index/indice that you left off at, and resume searching the other abstracts.  

Author

Commented:
I took your advice but am having other different problems.  I'm going to post another question for help with those.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial