When doing second call to repo method in success action of first then it
is giving some runtime exceptions
Explaination=>
1)I am giving first call to database and retrieving 10 records from
database and in success action storing those 10 records in cache and
adding those 10 records to UITableview and giving next call in success
action itself in service layer and adding those records in cache. 2)when
next time I will click on load more button then next 10 records will come
from cache and attaching next 10 records to uitableview and immediately
giving next call for retrieving next 10 records and storing those in
cache.So every time we will display records from cache so,It is loading
records very fastly but giving some runtime exceptions.
Code Section=> Service layer code- public void GetUserNewsFeed
(NewsFeedType feedType, Guid userId, int pageNumber, int pageSize, bool
getCachedData, Action successAction, Action errorAction) { try {
bool addNewsFeedToCache = false;
//We are using the same action method to execute in both the
cases i.e. for Everyone and Friends also
//We are going to create 2 separate methods for getting list
from Everyone and friend and going to save corresponding list
in cache
Action<FeedPagedList> serviceSuccessAction = (responseData) => {
//Second Success Action
Action<FeedPagedList> serviceNextSuccessAction =
(responseNextData) => {
if (responseNextData != null) {
FeedPagedList nextFeedPagedList =
(FeedPagedList)responseNextData;
if (nextFeedPagedList != null) {
if (nextFeedPagedList.FeedItemList != null &&
nextFeedPagedList.FeedItemList.Count > 0) {
//Add FeedPagedList in Cache
if (addNewsFeedToCache) {
CacheListContainer<FeedPagedList>
newsFeedContainer =
CachingServiceInstance.GetItemFromCache<CacheListContainer<FeedPagedList>>
(feedType.ToString ());
if (newsFeedContainer.DynamicList !=
null) {
if
(newsFeedContainer.DynamicList.FeedItemList
!= null &&
newsFeedContainer.DynamicList.FeedItemList.Count
> 0) {
foreach (FeedItem item in
nextFeedPagedList.FeedItemList)
{
newsFeedContainer.DynamicList.FeedItemList.Add
(item);
}
CachingServiceInstance.AddItemInCache<CacheListContainer<FeedPagedList>>
(feedType.ToString (),
newsFeedContainer);
}
}
}
}
}
}
} ;
//Second error Action
Action<bool> serviceNextErrorAction = (nextResponse) => {
errorAction (nextResponse);
} ;
if (responseData != null) {
FeedPagedList feedPagedList =
(FeedPagedList)responseData;
if (feedPagedList != null) {
//Add FeedPagedList in Cache
if (addNewsFeedToCache) {
CacheListContainer<FeedPagedList>
newsFeedContainer = new
CacheListContainer<FeedPagedList> ();
newsFeedContainer.DynamicList = feedPagedList;
CachingServiceInstance.AddItemInCache<CacheListContainer<FeedPagedList>>
(feedType.ToString (), newsFeedContainer);
}
List<FeedItem> scrapFeedItemlist =
feedPagedList.FeedItemList;
//Ordering the scrap item list by Scrap date only
if feedType is User.
//As if we sorted list for every one then in
Everyone's news tab the latest scrap displays at
the end or somewher else
if (feedPagedList.FeedItemList.Count > 0 &&
feedType == NewsFeedType.User)
scrapFeedItemlist =
scrapFeedItemlist.OrderByDescending (x =>
x.ScrapDate).ToList ();
NewsFeedViewModel viewModel = new
NewsFeedViewModel ();
viewModel.IsEveryone = feedType ==
NewsFeedType.Everyone ? true : false;
viewModel.FeedCount = feedPagedList.TotalRecords;
foreach (FeedItem scrapItem in scrapFeedItemlist) {
viewModel.Items.Add (NewsfeedAdapter
(scrapItem, userId));
}
successAction (viewModel);
//Second call for getting feed data
using(var pool = new NSAutoreleasePool())
{
try
{
pool.InvokeOnMainThread (delegate{
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.Everyone,
pageNumber+1, pageSize,
serviceNextSuccessAction,
serviceNextErrorAction);
} );
}
catch(Exception ex)
{
}
}
}
}
} ;
Action<bool> serviceErrorAction = (response) => {
errorAction (response);
} ;
switch (feedType) {
case NewsFeedType.Everyone:
if (getCachedData) {
CacheListContainer<FeedPagedList> newsfeedContainer =
CachingServiceInstance.GetItemFromCache<CacheListContainer<FeedPagedList>>
(CacheKey.Everyone.ToString ());
if (newsfeedContainer != null) {
FeedPagedList feedPagedList =
newsfeedContainer.DynamicList;
if (feedPagedList != null) {
List<FeedItem> newsFeeds =
feedPagedList.FeedItemList;
if (newsFeeds != null && newsFeeds.Count > 0) {
NewsFeedViewModel viewModel = new
NewsFeedViewModel ();
viewModel.IsEveryone = feedType ==
NewsFeedType.Everyone ? true : false;
viewModel.FeedCount =
feedPagedList.TotalRecords;
foreach (FeedItem scrapItem in newsFeeds) {
viewModel.Items.Add (NewsfeedAdapter
(scrapItem, userId));
}
successAction (viewModel);
using(var pool = new NSAutoreleasePool())
{
try
{
pool.InvokeOnMainThread (delegate{
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId,
NewsFeedType.Everyone,
pageNumber+1, pageSize,
serviceSuccessAction,
serviceErrorAction);
} );
}
catch(Exception ex)
{
}
}
} else {
ResetNewsFeedCache();
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.Everyone,
pageNumber,20, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.Everyone, pageNumber,
pageSize, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.Everyone, pageNumber,
pageSize, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList (userId,
NewsFeedType.Everyone, pageNumber, pageSize,
serviceSuccessAction, serviceErrorAction);
}
break;
case NewsFeedType.Friends:
if (getCachedData) {
CacheListContainer<FeedPagedList> newsfeedContainer =
CachingServiceInstance.GetItemFromCache<CacheListContainer<FeedPagedList>>
(CacheKey.Friends.ToString ());
if (newsfeedContainer != null) {
FeedPagedList feedPagedList =
newsfeedContainer.DynamicList;
if (feedPagedList != null) {
List<FeedItem> newsFeeds =
feedPagedList.FeedItemList;
if (newsFeeds != null && newsFeeds.Count > 0) {
NewsFeedViewModel viewModel = new
NewsFeedViewModel ();
viewModel.IsEveryone = feedType ==
NewsFeedType.Everyone ? true : false;
viewModel.FeedCount =
feedPagedList.TotalRecords;
foreach (FeedItem scrapItem in newsFeeds) {
viewModel.Items.Add (NewsfeedAdapter
(scrapItem, userId));
}
successAction (viewModel);
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.Friends, pageNumber,
pageSize, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.Friends, pageNumber,
pageSize, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.Friends, pageNumber,
pageSize, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList (userId,
NewsFeedType.Friends, pageNumber, pageSize,
serviceSuccessAction, serviceErrorAction);
}
break;
case NewsFeedType.User:
if (getCachedData) {
CacheListContainer<FeedPagedList> newsfeedContainer =
CachingServiceInstance.GetItemFromCache<CacheListContainer<FeedPagedList>>
(CacheKey.User.ToString ());
if (newsfeedContainer != null) {
FeedPagedList feedPagedList =
newsfeedContainer.DynamicList;
if (feedPagedList != null) {
List<FeedItem> newsFeeds =
feedPagedList.FeedItemList;
if (newsFeeds != null && newsFeeds.Count > 0) {
newsFeeds = newsFeeds.OrderByDescending (n
=> n.ScrapDate).ToList ();
NewsFeedViewModel viewModel = new
NewsFeedViewModel ();
viewModel.IsEveryone = feedType ==
NewsFeedType.Everyone ? true : false;
viewModel.FeedCount =
feedPagedList.TotalRecords;
foreach (FeedItem scrapItem in newsFeeds) {
viewModel.Items.Add (NewsfeedAdapter
(scrapItem, userId));
}
successAction (viewModel);
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.User, pageNumber,
pageSize, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.User, pageNumber,
pageSize, serviceSuccessAction,
serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList
(userId, NewsFeedType.User, pageNumber, pageSize,
serviceSuccessAction, serviceErrorAction);
}
} else {
addNewsFeedToCache = true;
ScrapBoomRepositoryInstance.GetNewsFeedList (userId,
NewsFeedType.User, pageNumber, pageSize,
serviceSuccessAction, serviceErrorAction);
}
break;
}
} catch (Exception ex) {
errorAction (true);
}
Screen layer code- private void LoadNewsFeedDetailsByMode (NewsFeedType
feedType,bool getCachedData) {
int pageNumber=0; if(feedType == NewsFeedType.Everyone)
pageNumber=EPageNumber; else if(feedType == NewsFeedType.Friends)
pageNumber=FPageNumber;
//Initializing success action which we call when we got results
from service
Action<NewsFeedViewModel> successAction = (responseData) => {
//Hide the loading indicator
this.HideActivityIndicator();
if(responseData != null)
{
//Converting response data into NewsFeedViewModel
NewsFeedViewModel newsFeedInstance =
(NewsFeedViewModel)responseData;
if(newsFeedInstance != null && newsFeedInstance.Items !=
null&&newsFeedInstance.Items.Count>0){
//Assigning response view model to screen model
ViewModel = newsFeedInstance;
//Creating an instance of Newsfeed cell item list as
we need cell item list for Table source
if(feedType == NewsFeedType.Everyone)
{
if(EveryoneCellItemList == null)
EveryoneCellItemList = new
List<NewsFeedCellItem> ();
if(newsFeedInstance.FeedCount>((EPageNumber+1)*(Configuration.PageSize)))
isLoadMoreEveryone=true;
else
isLoadMoreEveryone=false;
if(EveryoneCellItemList.Count == 0){
foreach (FeedItemViewModel feedItem in
newsFeedInstance.Items) {
EveryoneCellItemList.Add(new
NewsFeedCellItem(feedItem));
}
}
else
{
for(int i = 0; i <
newsFeedInstance.Items.Count; i++)
{
for(int j = 0; j <
EveryoneCellItemList.Count; j++)
{
if(newsFeedInstance.Items[i].ScrapbookItemId
==
EveryoneCellItemList[j].FeedItem.ScrapbookItemId)
EveryoneCellItemList[j].FeedItem =
newsFeedInstance.Items[i];
}
}
}
AddNewsFeedTable(EveryoneCellItemList);
}
else if(feedType == NewsFeedType.Friends)
{
if(FriendsCellItemList == null)
FriendsCellItemList = new
List<NewsFeedCellItem> ();
if(newsFeedInstance.FeedCount>((FPageNumber+1)*(Configuration.PageSize)))
isLoadMoreFriends=true;
else
isLoadMoreFriends=false;
if(FriendsCellItemList.Count == 0){
foreach (FeedItemViewModel feedItem in
newsFeedInstance.Items) {
FriendsCellItemList.Add(new
NewsFeedCellItem(feedItem));
}
}
else
{
for(int i = 0; i <
newsFeedInstance.Items.Count; i++)
{
for(int j = 0; j <
FriendsCellItemList.Count; j++)
{
if(newsFeedInstance.Items[i].ScrapbookItemId
==
FriendsCellItemList[j].FeedItem.ScrapbookItemId)
FriendsCellItemList[j].FeedItem =
newsFeedInstance.Items[i];
}
}
}
AddFriendsNewsFeedTable(FriendsCellItemList);
}
BeginInvokeOnMainThread(() => {
ReloadFeedsButton.Hidden = true;
} );
}
else
{
if(feedType == NewsFeedType.Everyone)
this.ShowInfoMessageAlertView("No Feeds Found.",
"There are no feeds available.");
else if(feedType == NewsFeedType.Friends)
this.ShowInfoMessageAlertView("No Feeds Found.",
"There are no feeds added by your friend.");
ShowReloadButton();
}
}
} ;
Action<bool> errorAction = (response) => {
this.HideActivityIndicator();
this.ShowErrorInProcessingAlertView();
//If this method is called using reload action then we need to
complete that action
if(ReloadCompleteAction != null)
ReloadCompleteAction();
ShowReloadButton();
} ;
//Calling API to get News feed details
this.ShowActivityIndicator();
AppDelegate.ScrapbookService.GetUserNewsFeed(feedType,
AppContext.Current.UserId,pageNumber,Configuration.PageSize,getCachedData,successAction,
errorAction);
}
private void LoadMoreFeedDetailsByMode (NewsFeedType feedType, bool
getCachedData)
{
int pageNumber=0;
if(feedType == NewsFeedType.Everyone)
pageNumber=EPageNumber;
else if(feedType == NewsFeedType.Friends)
pageNumber=FPageNumber;
Action<NewsFeedViewModel> successAction = (responseData) => {
BeginInvokeOnMainThread(()=>{
ActivitySpinner.StopAnimating();
} );
if(responseData != null)
{
//Converting response data into NewsFeedViewModel
NewsFeedViewModel newsFeedInstance =
(NewsFeedViewModel)responseData;
if(newsFeedInstance != null && newsFeedInstance.Items !=
null && newsFeedInstance.Items.Count>0){
//Assigning response view model to screen model
ViewModel = newsFeedInstance;
if(feedType == NewsFeedType.Everyone)
{
//Creating an instance of Newsfeed cell item only
if its null as we want to add new rows that we get
after calling load more
if(EveryoneCellItemList == null)
EveryoneCellItemList = new
List<NewsFeedCellItem> ();
if(newsFeedInstance.FeedCount>((EPageNumber+1)*(Configuration.PageSize)))
isLoadMoreEveryone=true;
else
isLoadMoreEveryone=false;
foreach (FeedItemViewModel feedItem in
newsFeedInstance.Items) {
EveryoneCellItemList.Add(new
NewsFeedCellItem(feedItem));
}
AddNewsFeedTable(EveryoneCellItemList);
}
else if(feedType == NewsFeedType.Friends)
{
//Creating an instance of Newsfeed cell item only
if its null as we want to add new rows that we get
after calling load more
if(FriendsCellItemList == null)
FriendsCellItemList = new
List<NewsFeedCellItem> ();
if(newsFeedInstance.FeedCount>((FPageNumber+1)*(Configuration.PageSize)))
isLoadMoreEveryone=true;
else
isLoadMoreFriends=false;
foreach (FeedItemViewModel feedItem in
newsFeedInstance.Items) {
FriendsCellItemList.Add(new
NewsFeedCellItem(feedItem));
}
AddFriendsNewsFeedTable(FriendsCellItemList);
}
BeginInvokeOnMainThread(() => {
ReloadFeedsButton.Hidden = true;
} );
}
else
{
if(feedType == NewsFeedType.Everyone)
this.ShowInfoMessageAlertView("No Feeds Found.",
"There are no feeds available.");
else if(feedType == NewsFeedType.Friends)
this.ShowInfoMessageAlertView("No Feeds Found.",
"There are no feeds added by your friend.");
ShowReloadButton();
}
}
} ;
Action<bool> errorAction = (response) => {
BeginInvokeOnMainThread(()=>{
ActivitySpinner.StopAnimating();
} );
//this.HideActivityIndicator();
this.ShowErrorInProcessingAlertView();
//If this method is called using reload action then we need to
complete that action
if(ReloadCompleteAction != null)
ReloadCompleteAction();
ShowReloadButton();
//Decrementing the pagenumber by 1 as error occurs while
getting feeds
if(feedType == NewsFeedType.Everyone)
EPageNumber--;
else if(feedType == NewsFeedType.Friends)
FPageNumber--;
} ;
//Calling API to get News feed details
BeginInvokeOnMainThread(()=>{
ActivitySpinner.StartAnimating();
} );
AppDelegate.ScrapbookService.GetUserNewsFeed(feedType,
AppContext.Current.UserId,pageNumber,Configuration.PageSize,getCachedData,successAction,
errorAction);
public void LoadMoreFeeds () { if (SelectedNewsFeedType ==
NewsFeedType.Everyone) { EPageNumber+=2; EveryoneCellItemList=null;
LoadMoreFeedDetailsByMode(SelectedNewsFeedType, true); } else
if(SelectedNewsFeedType == NewsFeedType.Friends) { FPageNumber+=2;
FriendsCellItemList=null; LoadMoreFeedDetailsByMode(SelectedNewsFeedType,
true); }
}
Error Section=>
1) * Assertion failure in -[ScrapBoom.iPhone.NewsFeedTableView
_endCellAnimationsWithContext:],
/SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:862
2)Unhandled Exception: MonoTouch.Foundation.MonoTouchException:
Objective-C exception thrown. Name: NSInternalInconsistencyException
Reason: attempt to delete row 62 from section 0 which only contains 10
rows before the update at (wrapper managed-to-native)
MonoTouch.ObjCRuntime.Messaging:void_objc_msgSendSuper_IntPtr_int
(intptr,intptr,intptr,int) at MonoTouch.UIKit.UITableView.ReloadRows
(MonoTouch.Foundation.NSIndexPath[] atIndexPaths, UITableViewRowAnimation
withRowAnimation) [0x00049] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UITableView.g.cs:558 at
ScrapBoom.iPhone.NewsFeedTableSource+c_AnonStorey2B.<>m_9B () [0x00017] in
/Users/volgainfotech/Projects/ScrapBoom/Kiln/ScrapBoom-Stable/ScrapBoom.iPhone/ScrapBoom.iPhone/Screens/iPhone/NewsFeed/NewsFeedTableSource.cs:73
at MonoTouch.Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in
/Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSAction.cs:87
at (wrapper managed-to-native)
MonoTouch.UIKit.UIApplication:UIApplicationMain
(int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main
(System.String[] args, System.String principalClassName, System.String
delegateClassName) [0x0004c] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 at
ScrapBoom.iPhone.Application.Main (System.String[] args) [0x00008] in
/Users/volgainfotech/Projects/ScrapBoom/Kiln/ScrapBoom-Stable/ScrapBoom.iPhone/ScrapBoom.iPhone/Main.cs:17
2013-08-06 18:44:21.906 ScrapBoomiPhone[1335:21d03] Unhandled managed
exception: Objective-C exception thrown. Name:
NSInternalInconsistencyException Reason: attempt to delete row 62 from
section 0 which only contains 10 rows before the update
(MonoTouch.Foundation.MonoTouchException) at (wrapper managed-to-native)
MonoTouch.ObjCRuntime.Messaging:void_objc_msgSendSuper_IntPtr_int
(intptr,intptr,intptr,int) at MonoTouch.UIKit.UITableView.ReloadRows
(MonoTouch.Foundation.NSIndexPath[] atIndexPaths, UITableViewRowAnimation
withRowAnimation) [0x00049] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UITableView.g.cs:558 at
ScrapBoom.iPhone.NewsFeedTableSource+c_AnonStorey2B.<>m_9B () [0x00017] in
/Users/volgainfotech/Projects/ScrapBoom/Kiln/ScrapBoom-Stable/ScrapBoom.iPhone/ScrapBoom.iPhone/Screens/iPhone/NewsFeed/NewsFeedTableSource.cs:73
at MonoTouch.Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in
/Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSAction.cs:87
at (wrapper managed-to-native)
MonoTouch.UIKit.UIApplication:UIApplicationMain
(int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main
(System.String[] args, System.String principalClassName, System.String
delegateClassName) [0x0004c] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 at
ScrapBoom.iPhone.Application.Main (System.String[] args) [0x00008] in
/Users/volgainfotech/Projects/ScrapBoom/Kiln/ScrapBoom-Stable/ScrapBoom.iPhone/ScrapBoom.iPhone/Main.cs:17
mono-rt: Stacktrace:
No comments:
Post a Comment