Android:来自Async Task的Mutliple线程导致问题

时间:2015-12-29 10:40:14

标签: java android multithreading parse-platform android-async-http

我正在从Parse SDK下载两个不同的图像 两张图片正确下载(我已经通过下载一种图像确认了它)
下载图像后,相同的图像被设置到lruCache中 我认为我的代码在Async Tas中存在一些问题,如下所示:

public partial class Google : Form
    {
        private string searchWord;

        public string password
        {
            get { return searchWord; }
            set { searchWord = Text; }
        }

        public Google()
        {
            InitializeComponent();
        }

        private void Google_Load(object sender, EventArgs e)
        {
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            searchBox.Text = searchWord;

            string search = searchBox.Text;

            StringBuilder add = new StringBuilder("https://www.google.com/#q=");
            add.Append(searchWord);

            googleWebbrowser.Navigate(add.ToString());
        }
    }
}

MyUtilties中的代码:

@Override
    protected String doInBackground(String... params) {
        try {
            ParseQuery<ParseObject> queryTable = ParseQuery.getQuery(TagClass.LOGIN_CREDENTIALS);
            List<ParseObject> objectNames = queryTable.find();
            lruCache = LRUCacheClass.getCache();
            for(ParseObject objectName:objectNames)
            {
                if(objectName.getString(TagClass.USER_ID).equals(params[0]))
                {
                    while(true) {
                        if (bitmapCoverPic == null) {
                            bitmapCoverPic = MyUtilities.decodeParseFileToBitmap(TagClass.COVER_PIC, (ParseFile) objectName.get(TagClass.COVER_PIC));
                        }
                        else
                        {
                            while(true) {
                                if(bitmapProfilePic==null) {
                                    bitmapProfilePic = MyUtilities.decodeParseFileToBitmap(TagClass.PROFILE_PICTURE, (ParseFile) objectName.get(TagClass.PROFILE_PICTURE));
                                }
                                else break;
                            }
                            break;
                        }
                    }
                    lruCache.put(TagClass.PROFILE_PICTURE,bitmapProfilePic);
                    lruCache.put(TagClass.COVER_PIC, bitmapCoverPic);
                }
            }
        }
        catch(ParseException parseException)
        {
            Log.e(TagClass.EXCEPTIONCATCH,"",null);
        }
        return "";
    }

我从片段中调用如下的异步任务:

public static Bitmap  decodeParseFileToBitmap(String type,ParseFile parseFile) throws ParseException
    {
        parseFile.getDataInBackground(new GetDataCallback() {

            @Override
            public void done(byte[] data, ParseException e) {
                if (e == null) {
                    bitmap = BitmapFactory
                            .decodeByteArray(data, 0, data.length);
                }
            }
        });
        return bitmap;
    }

1 个答案:

答案 0 :(得分:0)

尝试替换

while(true) {
                       if (bitmapCoverPic == null) {
                                bitmapCoverPic = MyUtilities.decodeParseFileToBitmap(TagClass.COVER_PIC, (ParseFile) objectName.get(TagClass.COVER_PIC));
                           }
                            else
                            {
                                while(true) {
                                    if(bitmapProfilePic==null) {
                                        bitmapProfilePic = MyUtilities.decodeParseFileToBitmap(TagClass.PROFILE_PICTURE, (ParseFile) objectName.get(TagClass.PROFILE_PICTURE));
                                        }
                                        else break;
                                    }
                                    break;
                                }
                            }
                            lruCache.put(TagClass.PROFILE_PICTURE,bitmapProfilePic);
                            lruCache.put(TagClass.COVER_PIC, bitmapCoverPic);

 bitmapProfilePic = MyUtilities.decodeParseFileToBitmap(TagClass.PROFILE_PICTURE, `(ParseFile) objectName.get(TagClass.PROFILE_PICTURE));`
bitmapCoverPic = MyUtilities.decodeParseFileToBitmap(TagClass.COVER_PIC, (ParseFile) objectName.get(TagClass.COVER_PIC));
if(bitmapProfilePic!=null){
lruCache.put(TagClass.PROFILE_PICTURE,bitmapProfilePic);
                                lruCache.put(TagClass.COVER_PIC, bitmapCoverPic);
  lruCache.put(TagClass.COVER_PIC, bitmapCoverPic);
}
if(bitmapCoverPic!=null{
 lruCache.put(TagClass.PROFILE_PICTURE,bitmapProfilePic);
}