public static IAsyncOperation<IEnumerable<object>> GetFeedsFromDBAsync()
        {
            return Task.Factory.StartNew(() =>
            {
                var v = new List<object>();
                using (var db = GetUserDB())
                {
                    var stmt = db.PrepareStatement("SELECT id, uri,title, image_uri FROM feeds");
                    while (stmt.HasMore())
                    {
                        int id = stmt.ColumnAsIntAt(0);
                        var fi = new FeedInfo(
                            stmt.ColumnAsTextAt(1),
                            stmt.ColumnAsTextAt(2),
                            stmt.ColumnAsTextAt(3));
                        fi.IsLoaded = true;
                        LoadFeedItemsFor(db, id, fi);
                        v.Add(fi);

                    }
                    return (IEnumerable<object>)v;
                }
            }).AsAsyncOperation();
        }
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            //
            // Associate a cancellation handler with the background task.
            //
            taskInstance.Canceled += OnCanceled;

            if (!ViewModelHelper.IsConnected)
            {
                return;
            }

            //
            // Get the deferral object from the task instance, and take a reference to the taskInstance.
            //
            _deferral = taskInstance.GetDeferral();

            var uris = PersistenceHelper.GetUserFeedsAsync().AsTask().Result;
            var feeds = new List<FeedInfo>();
            foreach (var uri in uris)
            {
                try
                {
                    var fi = new FeedInfo(uri);
                    fi.LoadNonGUI();
                    feeds.Add(fi);
                }
                catch
                {
                    Debug.WriteLine("Failed to load FeedInfo");
                }
            }

            PersistenceHelper.SaveFeedsToDBAsync(feeds).AsTask().Wait();
            _deferral.Complete();
        }
        public void LoadFeedsFromTheInternet()
        {
            _feeds = new ObservableVector<object>();
            PersistenceHelper.GetUserFeedsAsync().AsTask().ContinueWith(t =>
            {
                foreach (var uri in t.Result)
                {
                    try
                    {
                        var fi = new FeedInfo(uri);
                        fi.BeginLoad();
                        _feeds.Add(fi);
                    }
                    catch
                    {
                        Debug.WriteLine("Failed to load FeedInfo");
                    }
                }

                if (_feeds.Count > 0)
                {
                    SelectedFeed = (FeedInfo)_feeds[0];
                }
            });
        }
        private static void SaveFeedToDB(Database db, FeedInfo feed)
        {
            var stmt = db.PrepareStatement("INSERT INTO feeds (uri, title, image_uri) values (?,?,?)");
            stmt.BindText(1, feed.Uri.AbsoluteUri);
            stmt.BindText(2, feed.Title);
            if (feed.ImageUri != null)
            {
                stmt.BindText(3, feed.ImageUri.AbsoluteUri);
            }
            stmt.Execute();

            stmt = db.PrepareStatement("SELECT last_insert_rowid()");
            int feedId = 0;
            if (stmt.HasMore())
            {
                feedId = stmt.ColumnAsIntAt(0);
            }
            else
            {
                throw new Exception("Last inserted row id was not found in sqlite");
            }

            foreach (var feedItem in feed.FeedItems)
            {
                var fi = (FeedItem)feedItem;
                if (fi.FullTextLoaded)
                {
                    SaveFeedItemToDB(db, feedId, fi);
                }
            }
        }
        private static void LoadFeedItemsFor(Database db, int feedId, FeedInfo feedInfo)
        {
            var stmt = db.PrepareStatement("SELECT title, summary, uri, image_uri, text FROM feed_items where feed_id = ?");
            stmt.BindInt(1, feedId);
            while (stmt.HasMore())
            {
                FeedItem fi = new FeedItem(
                    stmt.ColumnAsTextAt(0),
                    stmt.ColumnAsTextAt(1),
                    stmt.ColumnAsTextAt(2),
                    stmt.ColumnAsTextAt(3),
                    stmt.ColumnAsTextAt(4));

                feedInfo.FeedItems.Add(fi);
            }
        }